python 字串常用操作

來源:互聯網
上載者:User

標籤:this   welcome   ring   use   love you   sign   support   replace   world   

字串常用方法capitalize()

String.capitalize() 將字串首字母變為大寫

name = ‘xiaoming‘new_name = name.capitalize()print(new_name) 

 

運行結果:
Xiaoming

count()

String.count() 統計字元出現的次數

name = ‘xiaoming‘name_num = name.count(‘i‘)print(name_num)  # 2

 

center()

String.center()

#列印輸出字元,讓字串放在中間name = ‘Libai‘print(name.center(50,‘*‘))

 

輸出結果如下:

**********************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()) # Trueprint(test_str02.isalnum()) # False

 

isalnum()方法判斷字串中是否全部為數字或者英文,符合就返回True,不符合就返回False,如果裡麵包含有符號或者空格之類的特殊字元也會返回False。

isalpha()

String.isalpha() 判斷字串中是否全部為純英文字元

test_str03 = ‘hello I love you‘test_str04 = ‘helloILoveYou‘print(test_str03.isalpha()) # Falseprint(test_str04.isalpha()) # True

 

isdigit()

String.isdigit() 判斷字串中是否全部為整數

# isdigit() 判斷是否為整數print(‘123‘.isdigit()) # Trueprint(‘hello‘.isdigit()) # False

 

isidentifier()

String.isidentifier() 判斷是不是一個合法的標識符

# isidentifier() 判斷是不是一個合法的標識符print(‘test‘.isidentifier()) # Trueprint(‘12‘.isidentifier()) # Falseprint(‘_aa‘.isidentifier()) # True

 

判斷字串是否全部為大寫或者小寫
# islower() 判斷字串是否全部是小寫print(‘Hello,world‘.islower()) # False# 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

 

python 字串常用操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.