10個(gè)有用的Python字符串函數(shù)小結(jié)
Python 字符串是一個(gè)內(nèi)置的類(lèi)型序列。字符串可用于處理 Python 中的文本數(shù)據(jù)。Python 字符串是 Unicode 點(diǎn)的不可變序列。在 Python 中創(chuàng)建字符串是最簡(jiǎn)單易用的。要在 Python 中創(chuàng)建字符串,我們只需將文本括在單引號(hào)和雙引號(hào)中。Python 對(duì)單引號(hào)和雙引號(hào)語(yǔ)句的處理方式相同。因此,在本文中,我們將討論 Python 中用于數(shù)據(jù)分析和數(shù)據(jù)操作的一些重要且有用的字符串函數(shù),主要用于自然語(yǔ)言處理(NLP)。
我們將在本文中討論的 Python 字符串函數(shù)如下:
一、capitalize() 函數(shù)
capitalize() 函數(shù)返回一個(gè)字符串,其中第一個(gè)字符是大寫(xiě)。
語(yǔ)法:string.capitalize()
示例 1:使給定句子中的第一個(gè)字母大寫(xiě)
string = "CSDN is the largest developer community in China" print(string.capitalize())
輸出:
Csdn is the largest developer community in china
示例 2:如果第一個(gè)字符是數(shù)字而不是字符會(huì)發(fā)生什么
string = '3th CSDN force plan activities are very good' print(string.capitalize())
輸出:
3th csdn force plan activities are very good
二、lower( ) 函數(shù)
lower() 函數(shù)返回一個(gè)字符串,其中給定字符串中的所有字符都是小寫(xiě)。這個(gè)函數(shù)對(duì)符號(hào)和數(shù)字沒(méi)有任何作用,即,只是忽略了這些東西。
語(yǔ)法:string.lower()
示例 1:小寫(xiě)給定字符串
string = "Haiyong is an excellent CSDN blogger" print(string.lower())
輸出:
haiyong is an excellent csdn blogger
示例 2: 如果有數(shù)字而不是字符會(huì)發(fā)生什么
string = '3th CSDN force plan activities are very good' print(string.lower())
輸出:
3th csdn force plan activities are very good
三、title( ) 函數(shù)
title() 函數(shù)返回一個(gè)字符串,其中字符串中每個(gè)單詞的第一個(gè)字符都是大寫(xiě)。它就像標(biāo)題或標(biāo)題。
如果字符串中的任何單詞包含數(shù)字或符號(hào),則此函數(shù)將其后的第一個(gè)字母轉(zhuǎn)換為大寫(xiě)。
語(yǔ)法:string.title()
示例 1:使每個(gè)單詞的第一個(gè)字母大寫(xiě)
string = "The blog you are reading will be on the hot list" print(string.title())
輸出:
The Blog You Are Reading Will Be On The Hot List
示例 2:如果有數(shù)字而不是字符會(huì)發(fā)生什么
string = '10 useful Python string functions you must know' print(string.title())
輸出:
10 Useful Python String Functions You Must Know
四、casefold() 函數(shù)
casefold() 函數(shù)返回一個(gè)字符串,其中所有字符都是小寫(xiě)。
這個(gè)函數(shù)類(lèi)似于lower()函數(shù),但是casefold()函數(shù)更強(qiáng)大,更激進(jìn),這意味著它將更多的字符轉(zhuǎn)換成小寫(xiě),并且在比較兩個(gè)字符串時(shí)會(huì)找到更多的匹配項(xiàng),并且都使用casefold()進(jìn)行轉(zhuǎn)換 功能。
語(yǔ)法:string.casefold()
示例 1:將給定的字符串變?yōu)樾?xiě)
string = "CSDN is the largest developer community in China" print(string.casefold())
輸出:
csdn is the largest developer community in china
示例 2:如果有數(shù)字而不是字符會(huì)發(fā)生什么
string = '10 useful Python string functions you must know' print(string.casefold())
輸出:
10 useful python string functions you must know
五、upper( ) 函數(shù)
upper() 函數(shù)返回一個(gè)字符串,其中給定字符串中的所有字符都為大寫(xiě)。這個(gè)函數(shù)對(duì)符號(hào)和數(shù)字沒(méi)有任何作用,即,只是忽略了這些東西。
語(yǔ)法:string.upper()
示例 1:給定字符串的大寫(xiě)
string = "CSDN is the largest developer community in China" print(string.upper())
輸出:
CSDN IS THE LARGEST DEVELOPER COMMUNITY IN CHINA
示例 2:如果有數(shù)字而不是字符會(huì)發(fā)生什么
string = '10 useful Python string functions you must know' print(string.upper())
輸出:
10 USEFUL PYTHON STRING FUNCTIONS YOU MUST KNOW
六、count( ) 函數(shù)
count() 函數(shù)查找指定值(由用戶(hù)給定)在給定字符串中出現(xiàn)的次數(shù)。
語(yǔ)法: string .count( value, start, end )
示例 1:返回值“CSDN ”在字符串中出現(xiàn)的次數(shù)
string = "CSDN is the largest developer community in China" print(string.count("CSDN "))
輸出:
1
示例 2: 返回值“CSDN ”在字符串中 從位置 8 到 16 出現(xiàn)的次數(shù)
string = "CSDN is the largest developer community in China" print(string.count("analytics", 8, 16))
輸出:
0
七、find( ) 函數(shù)
find() 函數(shù)查找指定值的第一次出現(xiàn)。如果在該字符串中找不到該值,則返回 -1。
find() 函數(shù)與 index() 函數(shù)幾乎相同,但唯一的區(qū)別是 index() 函數(shù)在找不到值時(shí)引發(fā)異常。
語(yǔ)法:string.find(value, start, end)
示例 1:文本中字母“d”第一次出現(xiàn)的位置是什么?
string = "CSDN is the largest developer community in China" print(string.find("d"))
輸出:
20
示例 2:僅在位置 5 和 16 之間搜索時(shí),字母“d”在文本中的哪個(gè)位置首次出現(xiàn)?
string = "CSDN is the largest developer community in China" print(string.find("d", 12, 22))
輸出:
20
示例 3:如果找不到該值,則 find() 函數(shù)返回 -1,但 index() 函數(shù)會(huì)引發(fā)異常
string = "CSDN is the largest developer community in China" print(string.find("d", 5, 10))
輸出:
-1
八、replace() 函數(shù)
replace() 函數(shù)用另一個(gè)指定的短語(yǔ)替換指定的短語(yǔ)。
注意:如果沒(méi)有指定任何其他內(nèi)容,所有出現(xiàn)的指定短語(yǔ)都將被替換。
語(yǔ)法: string .replace( oldvalue, newvalue, count )
示例 1:替換所有出現(xiàn)的單詞“developer ”
string = "CSDN is the largest developer community in China" print(string.replace("largest ", "best "))
輸出:
CSDN is the best developer community in China
示例 2:僅替換第一次出現(xiàn)的單詞“developer ”
string = "CSDN is China's largest developer community suitabsle for developer to learn" print(string.replace("developer ", "developers ", 1))
輸出:
CSDN is China's largest developers community suitabsle for developer to learn
九、swapcase( ) 函數(shù)
swapcase() 函數(shù)返回一個(gè)字符串,其中所有大寫(xiě)字母都是小寫(xiě)字母,反之亦然。
語(yǔ)法:string.swapcase()
示例 1:將小寫(xiě)字母變?yōu)榇髮?xiě),將大寫(xiě)字母變?yōu)樾?xiě)
string = "CSDN is the largest developer community in China" print(string.swapcase())
輸出:
csdn IS THE LARGEST DEVELOPER COMMUNITY IN cHINA
示例 2:如果有數(shù)字而不是字符會(huì)發(fā)生什么
string = '3th CSDN force plan activities are very good' print(string.swapcase())
輸出:
3TH csdn FORCE PLAN ACTIVITIES ARE VERY GOOD
十、join () 函數(shù)
join() 函數(shù)獲取可迭代對(duì)象中的所有項(xiàng)并將它們連接成一個(gè)字符串。我們必須指定一個(gè)字符串作為分隔符。
語(yǔ)法:string.join(iterable)
示例 1:將給定元組中的所有項(xiàng)連接成一個(gè)字符串,使用 #(hashtag)字符作為分隔符
myTuple = ("Computer Scientist", "Programming Learning", "Python Programming") x = " # ".join(myTuple) print(x)
輸出:
Computer Scientist # Programming Learning # Python Programming
示例2:將給定字典中的所有項(xiàng)目連接成一個(gè)字符串,使用單詞“TEST”作為分隔符
myDict = {"name": "CSDN", "country": "China", "Technology": "Python Programming"} mySeparator = "TEST" x = mySeparator.join(myDict) print(x)
輸出:
nameTESTcountryTESTTechnology
到此這篇關(guān)于10個(gè)有用的Python字符串函數(shù)小結(jié)的文章就介紹到這了,更多相關(guān)Python字符串函數(shù)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。