👓 前言
Python 字串是一個內置的型別序列,字串可用于處理 Python 中的文本資料,Python 字串是 Unicode 點的不可變序列,在 Python 中創建字串是最簡單易用的,要在 Python 中創建字串,我們只需將文本括在單引號和雙引號中,Python 對單引號和雙引號陳述句的處理方式相同,因此,在本文中,我們將討論 Python 中用于資料分析和資料操作的一些重要且有用的字串函式,主要用于自然語言處理(NLP),

我們將在本文中討論的 Python 字串函式如下:
Python 字串函式
- 👓 前言
- 😊 一、capitalize() 函式
- 💌 二、lower( ) 函式
- ? 三、title( ) 函式
- 🧿 四、casefold() 函式
- 🏀 五、upper( ) 函式
- 🏆 六、count( ) 函式
- 🎻 七、find( ) 函式
- 🎥 八、replace() 函式
- 🍖 九、swapcase( ) 函式
- ? 十、join () 函式
- 😊 結尾想說的
😊 一、capitalize() 函式
capitalize() 函式回傳一個字串,其中第一個字符是大寫,
語法:string.capitalize()
示例 1:使給定句子中的第一個字母大寫
string = "CSDN is the largest developer community in China"
print(string.capitalize())
輸出:
Csdn is the largest developer community in china
示例 2:如果第一個字符是數字而不是字符會發生什么
string = '3th CSDN force plan activities are very good'
print(string.capitalize())
輸出:
3th csdn force plan activities are very good
💌 二、lower( ) 函式
lower() 函式回傳一個字串,其中給定字串中的所有字符都是小寫,這個函式對符號和數字沒有任何作用,即,只是忽略了這些東西,
語法: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
? 三、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
🧿 四、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
🏀 五、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
🏆 六、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
🎻 七、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
🎥 八、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
🍖 九、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
? 十、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
😊 結尾想說的
我希望你喜歡這篇文章,如果你喜歡它,也分享給你的朋友,有未提及的內容或想分享您的想法請隨時在下面發表評論,我會盡快回復您,😉
我已經寫了很長一段時間的技術博客,并且主要通過CSDN發表,這是我的一篇python基礎教程,我喜歡通過文章分享技術與快樂,您可以訪問我的博客: https://haiyong.blog.csdn.net/ 以了解更多資訊,希望你們會喜歡!這里匯總了我的全部原創及作品原始碼:GitHub
如果你真的從這篇文章中學到了一些新東西,喜歡它,收藏它并與你的小伙伴分享,🤗最后,不要忘了?或📑支持一下哦
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/289296.html
標籤:python
