1 1資料型別 (string)

2022-08-29 11:12:14 字數 3266 閱讀 7956

字串建立

建立字串,只需給變數分配乙個值即可。例如:

first_str = 「hello world!!!」

second_str = 「hello python!!!」

字串訪問

訪問子字串,可以用方括號來擷取字串。例如:

first_str = "hello world!!!"

print "first_str[0]:",first_str[0]

first_str[0]: h

print "first_str[0:4]:%s"%(first_str[0:4])

first_str[0:4]:hell   #注意擷取結束邊界不算在擷取範圍內

字串更新

對已存在的字串修改,賦值給另乙個變數。例如:

first_str = "hello world!!!"

update_str = first_str[:7] + "python!!!"

print "update_st:%s"%(update_str)

update_str:hello wpython!!!

字串運算子: +、*、、[:]、in、not in 、r/r

a = 「hello」,b = "python"

操作符描述例項+

字串連線

>>> a  + b

"hello python"

*字串重複

>>>a * 2

"hellohello"

通過索引獲取字串中的字元

>>>a[1]

"e"[:]

擷取字串中的一部分

>>>a[1:4]

"ell"

in成員運算子,如果字串中包含給定的字元返回true

>>>"h" in a

true

not in

成員運算子,如果字串中不含給定字元返回true

>>>"m" not in a

true

r/r原始字元

>>>print r"\n"\n%

格式化字串

print "a:%s"%(a)

字串格式化

1、%實現,例如:

>>> print "my name is %s and weight is %d kg"%("luyangchun",48.6)

my name is luyangchun and weight is 48 kg

2、format實現

>>> print "my name is and weight is kg".format("luyangchun",48.6)

my name is luyangchun and weight is 48.6 kg

字串內建函式

1、split():通過指定分隔符對字串進行切片,如果引數num 有指定值,則僅分隔 num 個子字串

語法:string.split(str = "",num = string.count(str))

引數:stri--分隔符切片string,num--分割次數,有指定,則分割num個子字串 

返回:返回切割後的字串列表

例子:>>> split_str = "hello world,hello python,hello yangyang"

>>> split_list = split_str.split(",")

>>> split_list

['hello world', 'hello python', 'hello yangyang']

>>> split_num_list = split_str.split(",",1)

>>> split_num_list

['hello world', 'hello python,hello yangyang']

2、jion():用於將序列中的元素用指定的字元鏈結生成乙個新的字串

jion()語法:str.join(sequence)

引數:seauence--要連線的元素序列

返回值:通過指定字元連線序列中元素後生成的新的字串

例子:>>> str_list = ["hello world","hello python","hello yangyang"]

>>> join_str = ",".join(str_list)

>>> join_str

'hello world,hello python,hello yangyang'

3、replace():把字串中old str 替換成 new str,指定引數max,則替換不超過max次

語法:str.replace(old,new[,max])

引數:old--被替換的字串,new--新字串,max--可選字串,替換不超過max次

返回值:返回old替換成new後生成的新的字串

例子:>>> old_str = "hello world,hello python,hello yangyang"

>>> new_str = old_str.replace("hello","love")

>>> new_str

'love world,love python,love yangyang'

>>> new_max_str = old_str.replace("hello","love",2)

>>> new_max_str

'love world,love python,hello yangyang'

4、find():檢測字串中是否包含子字串str,若指定開始和結束範圍,則檢查是否包含在指定範圍內,若包含返回子字串的開始的索引值,否則返回-1

語法:string.find(str,beg,end)

引數:str--檢索字串,beg--開始索引,預設為0,end--結束索引,預設為字串長度

返回:如果包含子字串返回開始的索引值,否則返回-1

例子:>>> find_str = "hello world,hello python,hello yangyang"

>>> str_index = find_str.find("hello")

>>> str_index

0>>> str_index1 = find_str.find("hello",0,len(find_str))

>>> str_index1

0

資料型別 string

單引號建立字串 str0 php xust echo str0 echo 單引號中的單引號需要轉義 雙引號建立字串與單引號不同 可以解釋變數 str1 php echo str1 echo str3 php echo str3 echo 用heredoc 語法來建立字串 適用於大量字串的情況 優化的...

複雜的資料型別 string資料型別

include include 為了使用getline函式和在我們的程式中使用string型別,使用string型別時我們需要包含string字元標頭檔案 using namespace std define item 10 使用巨集定義變數 void calcsum void string int...

redis資料型別(String)

常用string操作命令 set keyvalue 設定值 getkey 獲取值 value 追加值 incr key 原子操作,自動加1 value必須為inter型別的字元。decr key 原子操作,自動減1 value必須為inter型別的字元。incrby key num 自動增加指定值 ...