10 個有用的 Python 字符串函數

這是我參與8月更文挑戰的第11天,活動詳情查看:8月更文挑戰python

👓 前言

Python 字符串是一個內置的類型序列。字符串可用於處理 Python 中的文本數據。Python 字符串是 Unicode 點的不可變序列。在 Python 中建立字符串是最簡單易用的。要在 Python 中建立字符串,咱們只需將文本括在單引號和雙引號中。Python 對單引號和雙引號語句的處理方式相同。所以,在本文中,咱們將討論 Python 中用於數據分析和數據操做的一些重要且有用的字符串函數,主要用於天然語言處理(NLP)。api

咱們將在本文中討論的 Python 字符串函數以下:markdown

😊 1、capitalize() 函數

capitalize() 函數返回一個字符串,其中第一個字符是大寫。app

語法:string.capitalize()ide

示例 1:使給定句子中的第一個字母大寫函數

string = "CSDN is the largest developer community in China" 
print(string.capitalize())
複製代碼

輸出:post

Csdn is the largest developer community in china
複製代碼

示例 2:若是第一個字符是數字而不是字符會發生什麼ui

string = '3th CSDN force plan activities are very good' 
print(string.capitalize())
複製代碼

輸出:lua

3th csdn force plan activities are very good
複製代碼

💌 2、lower( ) 函數

lower() 函數返回一個字符串,其中給定字符串中的全部字符都是小寫。這個函數對符號和數字沒有任何做用,即,只是忽略了這些東西。url

語法:string.lower()

示例 1:小寫給定字符串

string = "Haiyong is an excellent CSDN blogger" 
print(string.lower())
複製代碼

輸出:

haiyong is an excellent csdn blogger
複製代碼

示例 2: 若是有數字而不是字符會發生什麼

string = '3th CSDN force plan activities are very good' 
print(string.lower())
複製代碼

輸出:

3th csdn force plan activities are very good
複製代碼

⏰ 3、title( ) 函數

title() 函數返回一個字符串,其中字符串中每一個單詞的第一個字符都是大寫。它就像標題或標題。

若是字符串中的任何單詞包含數字或符號,則此函數將其後的第一個字母轉換爲大寫。

語法:string.title()

示例 1:使每一個單詞的第一個字母大寫

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:若是有數字而不是字符會發生什麼

string = '10 useful Python string functions you must know' 
print(string.title())
複製代碼

輸出:

10 Useful Python String Functions You Must Know
複製代碼

🧿 4、casefold() 函數

casefold() 函數返回一個字符串,其中全部字符都是小寫。

這個函數相似於lower()函數,可是casefold()函數更強大,更激進,這意味着它將更多的字符轉換成小寫,而且在比較兩個字符串時會找到更多的匹配項,而且都使用casefold()進行轉換 功能。

語法:string.casefold()

示例 1:將給定的字符串變爲小寫

string = "CSDN is the largest developer community in China" 
print(string.casefold())
複製代碼

輸出:

csdn is the largest developer community in china
複製代碼

示例 2:若是有數字而不是字符會發生什麼

string = '10 useful Python string functions you must know' 
print(string.casefold())
複製代碼

輸出:

10 useful python string functions you must know
複製代碼

🏀 5、upper( ) 函數

upper() 函數返回一個字符串,其中給定字符串中的全部字符都爲大寫。這個函數對符號和數字沒有任何做用,即,只是忽略了這些東西。

語法:string.upper()

示例 1:給定字符串的大寫

string = "CSDN is the largest developer community in China" 
print(string.upper())
複製代碼

輸出:

CSDN IS THE LARGEST DEVELOPER COMMUNITY IN CHINA
複製代碼

示例 2:若是有數字而不是字符會發生什麼

string = '10 useful Python string functions you must know' 
print(string.upper())
複製代碼

輸出:

10 USEFUL PYTHON STRING FUNCTIONS YOU MUST KNOW
複製代碼

🏆 6、count( ) 函數

count() 函數查找指定值(由用戶給定)在給定字符串中出現的次數。

語法: string .count( value, start, end )

示例 1:返回值「CSDN 」在字符串中出現的次數

string = "CSDN is the largest developer community in China" 
print(string.count("CSDN "))
複製代碼

輸出:

1
複製代碼

示例 2: 返回值「CSDN 」在字符串中 從位置 8 到 16 出現的次數

string = "CSDN is the largest developer community in China" 
print(string.count("analytics", 8, 16))
複製代碼

輸出:

0
複製代碼

🎻 7、find( ) 函數

find() 函數查找指定值的第一次出現。若是在該字符串中找不到該值,則返回 -1。

find() 函數與 index() 函數幾乎相同,但惟一的區別是 index() 函數在找不到值時引起異常。

語法:string.find(value, start, end)

示例 1:文本中字母「d」第一次出現的位置是什麼?

string = "CSDN is the largest developer community in China" 
print(string.find("d"))
複製代碼

輸出:

20
複製代碼

示例 2:僅在位置 5 和 16 之間搜索時,字母「d」在文本中的哪一個位置首次出現?

string = "CSDN is the largest developer community in China" 
print(string.find("d", 12, 22))
複製代碼

輸出:

20
複製代碼

示例 3:若是找不到該值,則 find() 函數返回 -1,但 index() 函數會引起異常

string = "CSDN is the largest developer community in China" 
print(string.find("d", 5, 10))
複製代碼

輸出:

-1
複製代碼

🎥 8、replace() 函數

replace() 函數用另外一個指定的短語替換指定的短語。

注意:若是沒有指定任何其餘內容,全部出現的指定短語都將被替換。

語法: string .replace( oldvalue, newvalue, count )

示例 1:替換全部出現的單詞「developer 」

string = "CSDN is the largest developer community in China" 
print(string.replace("largest ", "best "))
複製代碼

輸出:

CSDN is the best developer community in China
複製代碼

示例 2:僅替換第一次出現的單詞「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 複製代碼

🍖 9、swapcase( ) 函數

swapcase() 函數返回一個字符串,其中全部大寫字母都是小寫字母,反之亦然。

語法:string.swapcase()

示例 1:將小寫字母變爲大寫,將大寫字母變爲小寫

string = "CSDN is the largest developer community in China" 
print(string.swapcase())
複製代碼

輸出:

csdn IS THE LARGEST DEVELOPER COMMUNITY IN cHINA
複製代碼

示例 2:若是有數字而不是字符會發生什麼

string = '3th CSDN force plan activities are very good' 
print(string.swapcase())
複製代碼

輸出:

3TH csdn FORCE PLAN ACTIVITIES ARE VERY GOOD
複製代碼

✨ 10、join () 函數

join() 函數獲取可迭代對象中的全部項並將它們鏈接成一個字符串。咱們必須指定一個字符串做爲分隔符。

語法:string.join(iterable)

示例 1:將給定元組中的全部項鍊接成一個字符串,使用 #(hashtag)字符做爲分隔符

myTuple = ("Computer Scientist", "Programming Learning", "Python Programming") 
x = " # ".join(myTuple) 
print(x)
複製代碼

輸出:

Computer Scientist # Programming Learning # Python Programming
複製代碼

示例2:將給定字典中的全部項目鏈接成一個字符串,使用單詞「TEST」做爲分隔符

myDict = {"name": "CSDN", "country": "China", "Technology": "Python Programming"} 
mySeparator = "TEST" 
x = mySeparator.join(myDict) 
print(x)
複製代碼

輸出:

nameTESTcountryTESTTechnology
複製代碼

😊 結尾想說的

我但願你喜歡這篇文章。若是你喜歡它,也分享給你的朋友。有未說起的內容或想分享您的想法請隨時在下面發表評論,我會盡快回復您。😉

我已經寫了很長一段時間的技術博客,而且主要經過掘金髮表,這是個人一篇python基礎教程。我喜歡經過文章分享技術與快樂。您能夠訪問個人博客: 掘金-海擁 以瞭解更多信息。但願大家會喜歡!

若是你真的從這篇文章中學到了一些新東西,喜歡它,收藏它並與你的小夥伴分享。🤗最後,不要忘了❤或📑支持一下哦

相關文章
相關標籤/搜索