python字串詳解

2022-08-17 02:12:15 字數 3442 閱讀 4142

必選掌握

#isupper判斷字串是否全部都是大寫

str1 = 'hello,world'

str2 = 'hello,world'

print(str1.isupper())       false

print(str2.isupper())          true

#islower判斷字串是否全部都是小寫

str1 = 'hello,world'

str2 = 'hello,world'

print(str1.islower())       false

print(str2.islower())          true

#isalnum判斷字串裡是否有數字、字母、漢字.(或判斷字串裡是否有特殊字元)

str1 = '11122333aaah郝'

str2 = '11122333/qaaa'

print(str1.isalnum())          true

print(str2.isalnum())         false

#isdigit判斷字串裡面是否是整型

str1 = '123'

print(str1.isdigit())

true

#upper()方法把字串全部變成大寫

str = 'hello,world'

print(str.upper())

hello,world

#startswith判斷字串開頭是否為he

print(str1.startswith('he'))

#endswith判斷字串結尾是否為ld

print(str1.endswith('ld'))

#index取字串o的下標,如果沒有這個字元會報錯

str1 = 'hello,world'

print(str1.index('o'))

4print(str1.rindex('o'))

7#find取字串o的下標,如果沒有這個字元返回-1

str1 = 'hello,world'

print(str1.find('o'))

4print(str1.rfind('o'))

7print(str1.find('y'))

-1#isalpha判斷字串裡是英文或漢字

str1 = 'hello,world'

print(str1.isalpha())

false

#count統計字串裡l字元的個數

print(str1.count('l'))

3#istitle判斷是否是抬頭

抬頭判斷標準:連續不間斷單詞首字母大寫,其餘為小寫,否則為false

print(str1.istitle())

true

#把乙個字串變成抬頭

print(str1.title())

#isspace判斷字串是否是純空格

str1 = ''"

str2 = ''   "

print(str1.isspace()) 

false

print(str2.isspace())

ture

# replace替換字串o成sb,並且只替換1次

str1 = 'hello,world'

res = str1.replace('o','sb',1)

print(res)

hellsb,world

#把乙個可迭代物件(列表,元組,集合,字典,字串)變成字串,表中資料型別為字串

res = ''.join(['abc','123','吉喆'])

print(res)

print(type(res))

abc123吉喆

# str1 = '192.168.250.250'

把乙個字串從左往右切分變成列表(.代表切分點,1代表切分1次)

res = str1.split('.',1)

print(res)

['192', '168.250.250']

#把乙個字串從右往左切分變成列表(.代表切分點,1代表切分1次)

res = str1.rsplit('.',1)

print(res)

['192.168.250', '250']

#去除字串左右兩邊指定的字元(注意:必須為兩邊邊界)

str1 = '++++++hello,world*****'

res = str1.strip('=')

print(res)

++++++hello,world

print(str1.strip('+'))

hello,world*****

#去除字串右邊指定的字元

res = str1.rstrip('=')

print(res)

#去除字串左邊指定的字元

str1.lstrip('+')

#format將字串格式化,可以有以下3種格式

str1 = 'my name is {},my age is {}'

res = str1.format('吉喆', '23')

str1 = 'my name is ,my age is '

res = str1.format('23', '李凱')

str1 = 'my name is ,my age is '

res = str1.format(name='李凱', age='23')

print(res)

#%s,%d,%f可以格式化字串

str1 = 'my name is %s, my age is %s'

res = str1 % ('吉喆', 23)

print(res)

my name. is 吉喆, my age is 23

#利用索引或者下標取值,超出範圍報錯

str1 = 'hello,world'

print(str1[-100])

#字串的拼接

print(str1[4]+str1[5])

o,print('1'+'2')

12#切片

str1 = 'hello,world'

res = str1[2:5]#正向切片顧頭不顧尾

print(res)

llo# res = str1[-4:-1]#顧尾不顧頭

print(res)

rld# res = str1[:3]#索引為3往右的字元不要了(包括下標為3的字元)

print(res)

hel# res = str1[3:]#索引為3往左的字元不要了(不包括下標為3的字元)

print(res)

lo,world

# res = str1[::2]#步長為2,隔乙個字元取乙個字元

print(res)

hlowrd

Python字串詳解

一 字串 是乙個有序的字元的集合,用於儲存和表示基本的文字資訊,一對單 雙 或三引號中間包含的內容稱之為字串 1 建立 s hello word 2 特性 有序 不可變。有序 有順序的集合,就像列表的索引值有順序的排列。不可變 一旦宣告不可改變。不可改變的是記憶體id。id s 檢視記憶體id。注 ...

Python字串詳解

簡單地理解,字串就是 一串字元 也就是用引號內的任何資料,比如 hello,charlie 是乙個字串,how are you?也是乙個字串。python 要求,字串必須使用引號括起來,可以使用單引號或者雙引號,只要成對即可。字串中的內容幾乎可以包含任何字元,英文本元也行,中文字元也行。至於字串是用...

Python 字串方法詳解

python 字串方法詳解 型別 方法 註解 填充 center width fillchar ljust width fillchar rjust width fillchar zfill width expandtabs tabsize l fillchar 引數指定了用以填充的字元,預設為空格...