字符串經常使用方法 python
capitalize() git
String.capitalize() 將字符串首字母變爲大寫 api
-
name = 'xiaoming'
-
-
new_name = name.capitalize()
-
-
print(new_name)
運行結果:
Xiaoming 數組
count() ide
String.count() 統計字符出現的次數 this
-
name = 'xiaoming'
-
-
name_num = name.count('i')
-
-
print(name_num) # 2
center() 編碼
String.center() spa
#打印輸出字符,讓字符串放在中間 code
-
name = 'Libai'
-
print(name.center(50,'*'))
輸出結果以下: orm
**********************Libai***********************
endswith()
String.endswith() 判斷是否以指定的字符串結尾
-
name = 'Libai'
-
-
new_val = name.endswith('bai')
-
print(new_val)
結果爲:
True
find()
String.find() 查找字符串在原字符串中的位置,返回所在索引值
-
name = 'this is test plaintext'
-
-
print(name.find('this'))
-
print(name.find('is'))
在find()方法中,一樣可使用切片。
-
name = 'this is test plaintext'
-
-
test_val = name[name.find('test'):12]
-
-
print(test_val) #test
字符串的切片用法與列表的使用方式一致。
format()
-
String.format() 輸出指定的內容
-
user_show_name = 'hello,{name},welcome to here,do you like ,{name}'
-
-
print(user_show_name.format(name='yanyan'))
輸出效果以下:
hello,yanyan,welcome to here,do you like ,yanyan
format_map()
-
String.format_map() 將字典中的參數傳遞進字符串中,輸出
-
hello = "My name is {name},I am {age} years old.I like {hobby}"
-
-
# 使用format_map()方法來傳遞值
-
print(hello.format_map({'name':'yanyan','age':19,'hobby':'music travel'}))
isalnum()
String.isalnum() 判斷字符串中是否所有爲數字或者英文
-
test_str01 = 'helloIam19yearsold'
-
test_str02 = 'hello,I am 19 years old'
print(test_str01.isalnum()) # True
print(test_str02.isalnum()) # False
isalnum()方法判斷字符串中是否所有爲數字或者英文,符合就返回True,不符合就返回False,若是裏面包含有符號或者空格之類的特殊字符也會返回False。
isalpha()
String.isalpha() 判斷字符串中是否所有爲純英文字符
-
test_str03 = 'hello I love you'
-
test_str04 = 'helloILoveYou'
print(test_str03.isalpha()) # False
print(test_str04.isalpha()) # True
isdigit()
String.isdigit() 判斷字符串中是否所有爲整數
# isdigit() 判斷是否爲整數
-
print('123'.isdigit()) # True
-
print('hello'.isdigit()) # False
isidentifier()
String.isidentifier() 判斷是否是一個合法的標識符
# isidentifier() 判斷是否是一個合法的標識符
-
print('test'.isidentifier()) # True
-
print('12'.isidentifier()) # False
-
print('_aa'.isidentifier()) # True
判斷字符串是否所有爲大寫或者小寫
islower
# islower() 判斷字符串是否所有是小寫
-
print('Hello,world'.islower()) # False
isupper
# isupper() 判斷字符串是否所有爲大寫
-
print('Hello,world'.isupper()) # False
join()
sep.join(seq) 鏈接字符串數組。將字符串、元組、列表中的元素以指定的字符(分隔符)鏈接生成一個新的字符串
-
# 建立一個列表
-
name = ['張學友','劉德華','郭富城','黎明']
-
-
print('--'.join(name))
輸出結果以下:
張學友--劉德華--郭富城--黎明
ljust()
String.ljust(size,替換符號) 從前向後開始計算,當字符串的長度超過size時,超過部分用替換符號替代
rjust()
String.rjust(size,替換符號) 從後向前開始計算,當字符串的長度超過size時,超過部分用替換符號替代
lower 將字符串大寫變成小寫
String.lower()
-
# 建立一個字符串
-
str = "hello,I am LiBai,I am 23 years old ,I like travel"
-
-
# lower 將字符串大寫變成小寫
-
print(str.lower())
upper 將字符串小寫變成大寫
String.upper()
-
# 建立一個字符串
-
str = "hello,I am LiBai,I am 23 years old ,I like travel"
-
-
# 將字符串小寫變成大寫
-
print(str.upper())
Tip:上面的lower()方法和upper()方法改變字符串後將改變的結果返回,可是本來的字符串並不會改變。
lstrip 去掉字符串左邊的空格或者回車
String.lstrip()
-
print('-----------')
-
# 建立一個字符串
-
str = "\nhello,I am LiBai,I am 23 years old ,I like travel"
-
-
print(str.lstrip())
輸出結果以下:
-----------
hello,I am LiBai,I am 23 years old ,I like travel
除了lstrip 還有rstrip和 strip方法。
replace 替換
String.replace(old,new,count) 將字符串中的old字符替換爲New字符,count爲替換的個數
-
str = 'hello,world,hello'
-
-
print(str.replace('hello','Hello',1))
輸出的效果以下:
Hello,world,hello
split
String.split() 切割
str = 'hello,world,hello'
-
# 默認以空格爲分割
-
print(str.split()) # ['hello,world,hello'] 單詞之間沒有空格,因此全部的內容爲一個元素
-
# 以o爲分割
-
print(str.split('o')) # ['hell', ',w', 'rld,hell', '']
-
# 以逗號分割
-
print(str.split(',')) # ['hello', 'world', 'hello']
splitlines() 以換行爲分割
String.splitlines()
-
str = 'hello,\nworld,\nhello'
-
-
print(str.splitlines()) # ['hello,', 'world,', 'hello']
Tip:補充,python中的字符串並不容許修改值,只容許覆蓋值。
狀況以下:
-
# 建立字符串
-
str = 'hello,world'
-
print(str[0]) # h
-
-
# 嘗試去修改
-
str[0] = 'H'
-
print(str) # TypeError: 'str' object does not support item assignment
-
-
-
# 下面這種狀況是咱們常見的狀況,實際上是屬於一種字符串以前的值被新的值覆蓋掉了
-
str = 'Hello,YanYan'
-
print(str) # Hello,YanYan
總結
-
name = " Ian is very good at\t {st} learning and eager to learn {sm2}."
-
-
print(name.capitalize()) #本來大寫改爲小寫,本來小寫改爲大寫
-
-
print(name.count("a")) #查找裏面字符"a"的數量
-
-
print(name.center(100,"-")) #第一個數字爲總共打印多少字符,不夠用-補充
-
-
print(name.ljust(100,'*')) #左邊的填充符號
-
-
print(name.rjust(100,'*')) #右邊的填充符號
-
-
print(name.expandtabs(tabsize=20)) #把tab符號的長度限定爲20字節
-
-
print(name.endswith("rn")) #這個是一個判斷輸出是,True and False
-
-
print(name.find("is")) #查找是第幾個字符開始
-
-
print(name[name.find("is"):]) #這個就能夠切片了
-
-
print(name.format(st = 'learning',sm2 = 'organization')) #就是之前學過的添加
-
-
#name.encode() #將字符串編碼成bytes格式
-
-
print('asd213'.isalpha()) #判斷是否爲純英文字符
-
-
print('asd213'.isalnum()) #判斷是否爲英文字母以及0-9
-
-
print('asd2221'.isdigit()) #重點,判斷是否爲整數
-
-
print('Aa1'.isidentifier()) #判斷是不是合法的標識符,知足Python命名的變量名規則
-
-
print('sdf'.islower()) #判斷是不是小寫
-
-
print('ASA'.isupper()) #判斷是不是全是大寫
-
-
print('Asdf'.isnumeric()) #判斷是否只包含數字
-
-
print('Asdf'.istitle()) #判斷是否每一個首字母大寫
-
-
print('| '.join(['daf','fdaf','dfa'])) #很常見,將前面的字符加到後面的列表的中間
-
-
print('ALSX'.lower()) #把大寫變成小寫
-
-
print('ALads'.upper()) #把字符全是變成大寫
-
-
print('Asfdf\n \n'.strip()) #去掉兩邊的空格和回車
-
-
print('\nAsfdf\n'.lstrip()) #去掉左邊的空格和回車
-
-
print('Asfdf\n'.rstrip()) #去掉右邊的空格和回車
-
print('\nAsfdf\n'.lstrip()) #去掉左邊的空格和回車
-
-
print('Asfdf\n'.rstrip()) #去掉右邊的空格和回車
-
-
print('dfadda'.replace('a','D',1)) #替換,前面的被後面的替換,若是隻想換一個就加一個數字
-
-
print('ffadsfaadfaaaa'.rfind('f')) #找到最後一個f,並返回是第幾位
-
-
print('dafd fa df'.split(' ')) #利用空格進行分割,變成列表
-
-
print('VIC YI'.swapcase()) #大寫變成小寫
-
-
print('afdfa fad'.title()) #把每一個單詞首字母大寫