Python探索記 10 字串 下

2021-09-22 20:29:22 字數 4489 閱讀 5960

關於字串的常見操作,請參見如下示例:

# @time    : 2017/7/2 21:26

# @desc : string常見操作

string='hello,my name is hanmeimei'

'''find()

判斷子串是否在string中,若存在則返回子串在string中開始的索引值,否則返回-1

rfind()類似於find()函式,不過是從右邊開始查詢.

'''index1=string.find('llo')

print('index1=',index1)

#利用第二個,第三個引數指定判斷的範圍

index2=string.find('llo',0,10)

print('index2=',index2)

index3=string.find('llo',7,13)

print('index3=',index3)

print('= '*15)

'''count()

統計子串在string**現的次數

'''count1=string.count('m')

#利用第二個,第三個引數指定統計的範圍

count2=string.count('m',0,8)

print('count1=',count1)

print('count2=',count2)

print('= '*15)

'''replace()

把string中的str1替換成str2,如果count指定,則替換次數不超過count

'''replaceresult1=string.replace('mei','xx')

print('replaceresult1=',replaceresult1)

replaceresult2=string.replace('mei','xx',1)

print('replaceresult2=',replaceresult2)

print('string=',string)

print('= '*15)

'''split()

以str為分隔符切割string,可用maxsplit指定分割次數,即分隔成 maxsplit + 1 個子字串

'''fragment1=string.split(' ')

print('fragment1=',fragment1)

fragment2=string.split(' ',2)

print('fragment2=',fragment2)

print('= '*15)

'''startswith()

判斷string是否以str開頭

'''result1=string.startswith('hello')

result2=string.startswith('ello')

print('result1=',result1)

print('result2=',result2)

print('= '*15)

'''endswith()

判斷string是否以str結尾

'''result3=string.endswith('mei')

result4=string.endswith('***')

print('result3=',result3)

print('result4=',result4)

print('= '*15)

'''upper()

將string中字母變成大寫

'''upperstring=string.upper()

print('upperstring=',upperstring)

print('= '*15)

'''lower()

將string中字母變成小寫

'''lowerstring=upperstring.lower()

print('lowerstring=',lowerstring)

print('= '*15)

'''strip()

刪除字串兩端的空格。

類似的方法有lstrip(),rstrip()刪除字串左側,右側的空格

'''name=' hello all '

stripresult=name.strip()

print('stripresult=',stripresult)

print('= '*15)

'''partition()

把string以str分割成三部分,str前,str和str後

rpartition()類似於 partition()函式,不過是從右邊開始.

'''fragments=string.partition('llo')

print('fragments=',fragments)

print('= '*15)

'''splitlines()

按照行分隔即\n,返回乙個包含各行作為元素的列表

'''lines="good\nmorning\nsir"

resultlines=lines.splitlines()

print('resultlines=',resultlines)

print('= '*15)

'''isalpha()

判斷string中是否全為字母

'''isalpha=string.isalpha()

print('isalpha=',isalpha)

print('= '*15)

'''isdigit()

判斷string中是否全為數字

'''digitstring='123456789'

isdigit=digitstring.isdigit()

print('isdigit=',isdigit)

print('= '*15)

'''join()

str.join(sequence)將字串、元組、列表中的元素以指定的字元(str)連線生成乙個新的字串

'''str='_'

sequence=['hello','everyone','thanks']

joinresult=str.join(sequence)

print('joinresult=',joinresult)

print('= '*15)

測試結果如下:

index1= 2

index2= 2

index3= -1

= = = = = = = = = = = = = = =

count1= 4

count2= 1

= = = = = = = = = = = = = = =

replaceresult1= hello,my name is han***x

replaceresult2= hello,my name is hanxxmei

string= hello,my name is hanmeimei

= = = = = = = = = = = = = = =

fragment1= ['hello,my', 'name', 'is', 'hanmeimei']

fragment2= ['hello,my', 'name', 'is hanmeimei']

= = = = = = = = = = = = = = =

result1= true

result2= false

= = = = = = = = = = = = = = =

result3= true

result4= false

= = = = = = = = = = = = = = =

upperstring= hello,my name is hanmeimei

= = = = = = = = = = = = = = =

lowerstring= hello,my name is hanmeimei

= = = = = = = = = = = = = = =

stripresult= hello all

= = = = = = = = = = = = = = =

fragments= ('he', 'llo', ',my name is hanmeimei')

= = = = = = = = = = = = = = =

resultlines= ['good', 'morning', 'sir']

= = = = = = = = = = = = = = =

isalpha= false

= = = = = = = = = = = = = = =

isdigit= true

= = = = = = = = = = = = = = =

joinresult= hello_everyone_thanks

= = = = = = = = = = = = = = =

Python探索記 09 字串 上

在python中用單引號或者雙引號表示字串 time 2017 7 2 20 57 desc 字串string 字串的表示方式 name 杉原杏璃 nickname 沖田杏梨 print name name print nickname nickname print name s,nickname ...

Python探索記 09 字串 上

在python中用單引號或者雙引號表示字串 time 2017 7 2 20 57 desc 字串string 字串的表示方式 name 杉原杏璃 nickname 沖田杏梨 print name name print nickname nickname print name s,nickname ...

10)字串知識

字元 0 ascii值是0 但是0的ascii值是48,所以 strlen遇到 0 就停止 但是 sizeof是測得陣列的長度,包含 0 和0 滑油輸出時 s 也是,遇到 0 就停止輸出了 strcpy 是會把字串的那個 0複製過去的 比如 main 列印出 x y z 1 2 3 4 9 可以看出...