欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解python字符串相關(guān)str

 更新時(shí)間:2022年01月16日 11:01:09   作者:cw_hstx  
這篇文章主要為大家介紹了python字符串相關(guān)str,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1:訪str單個(gè)字符

#for循環(huán)迭代
name = 'Chengwei'
for ch in name:
    print(ch, end=' ')
#C h e n g w e i
#索引
print(name[1])
#h
print(name[-1]) #最后一個(gè)為 -1
#i
#len()函數(shù)返回str字符串?dāng)?shù)量
print(len(name))
#8

2: 字符串連接

message = 'hello' + 'world'
print(message)
#helloworld

3:str切片

message = 'helloworld'
print(message[2:4])
#ll

4:使用in 和not in 測試字符串

message = 'helloworld'
if 'hello' in message:
    print('YES')
#YES
if 'aaa' not in message:
    print('YES')
#YES

5:str方法

字符串測試方法

isalnum()如果str只包含字母或數(shù)字,并且長度至少為一個(gè)字符,則返回true。否則返回false
isalpha()如果str只包含字母并且長度至少為一個(gè)字符,則返回true。否則返回false
isdigit()如果str只包含數(shù)字如果str只包含字母并且長度至少為一個(gè)字符,則返回true。否則返回false
islower()

如果str中的所有字母都是小寫如果str只包含數(shù)字如果str只包含字母并且長度至少為一個(gè)字符,則返回true。否則返回false

isspace()如果str只包含空白字符,并且長度至少為一個(gè)字符,則返回true。否則返回false(空格,\n,\t)
isupper()

如果str中的所有字母都是大寫如果str只包含數(shù)字如果str只包含字母并且長度至少為一個(gè)字符,則返回true。否則返回false

字符串修改方法

lower()返回將所有字母轉(zhuǎn)換為小寫的str副本。不是字母的不變
lstrip()返回刪除所有前導(dǎo)空白字符的str副本。
lstrip(str_item)str_item是字符串。返回刪除所有前導(dǎo)str_item的字符串副本
rstrip()返回所有尾部空白字符串的字符串副本。
rstrip(str_item)返回刪除所有尾部str_item的字符串副本
strip() 
strip(str_item)刪除所有前導(dǎo)和尾部str_item的字符串副本
upper() 
lower()返回將所有字母轉(zhuǎn)換為小寫的str副本。不是字母的不變
lstrip()返回刪除所有前導(dǎo)空白字符的str副本。
lstrip(str_item)str_item是字符串。返回刪除所有前導(dǎo)str_item的字符串副本
rstrip()返回所有尾部空白字符串的字符串副本。
rstrip(str_item)返回刪除所有尾部str_item的字符串副本
strip() 
strip(str_item)刪除所有前導(dǎo)和尾部str_item的字符串副本
upper() 

搜索和替換的方法

endswith(substring)返回true或false
find(substring)返回找到substring的最小索引位置。沒有找到返回 -1
replace(old, new)返回將所有的old替換為new的字符串副本
starstwith(substring)返回true或false

6:重復(fù)操作符

print('hello' * 2)
#hellohello

7:分割字符串

message = 'hello world chengwei'
message_list = message.split()
print(message_list)
#['hello', 'world', 'chengwei']
message = 'hello,world,chengwei'
message_list = message.split(',')
print(message_list)
#['hello', 'world', 'chengwei']

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評論