五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

10個有用的Python字符串函數(shù)小結(jié)

發(fā)布日期:2022-02-11 15:47 | 文章來源:腳本之家

Python 字符串是一個內(nèi)置的類型序列。字符串可用于處理 Python 中的文本數(shù)據(jù)。Python 字符串是 Unicode 點的不可變序列。在 Python 中創(chuàng)建字符串是最簡單易用的。要在 Python 中創(chuàng)建字符串,我們只需將文本括在單引號和雙引號中。Python 對單引號和雙引號語句的處理方式相同。因此,在本文中,我們將討論 Python 中用于數(shù)據(jù)分析和數(shù)據(jù)操作的一些重要且有用的字符串函數(shù),主要用于自然語言處理(NLP)。

我們將在本文中討論的 Python 字符串函數(shù)如下:

一、capitalize() 函數(shù)

capitalize() 函數(shù)返回一個字符串,其中第一個字符是大寫。
語法:string.capitalize()

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

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

輸出:
Csdn is the largest developer community in china

示例 2:如果第一個字符是數(shù)字而不是字符會發(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ù)返回一個字符串,其中給定字符串中的所有字符都是小寫。這個函數(shù)對符號和數(shù)字沒有任何作用,即,只是忽略了這些東西。
語法:string.lower()
示例 1:小寫給定字符串

string = "Haiyong is an excellent CSDN blogger" 
print(string.lower())

輸出:
haiyong is an excellent csdn blogger

示例 2: 如果有數(shù)字而不是字符會發(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ù)返回一個字符串,其中字符串中每個單詞的第一個字符都是大寫。它就像標題或標題。
如果字符串中的任何單詞包含數(shù)字或符號,則此函數(shù)將其后的第一個字母轉(zhuǎn)換為大寫。
語法: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:如果有數(shù)字而不是字符會發(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ù)返回一個字符串,其中所有字符都是小寫。
這個函數(shù)類似于lower()函數(shù),但是casefold()函數(shù)更強大,更激進,這意味著它將更多的字符轉(zhuǎn)換成小寫,并且在比較兩個字符串時會找到更多的匹配項,并且都使用casefold()進行轉(zhuǎn)換 功能。
語法:string.casefold()

示例 1:將給定的字符串變?yōu)樾?br/>

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

輸出:
csdn is the largest developer community in china

示例 2:如果有數(shù)字而不是字符會發(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ù)返回一個字符串,其中給定字符串中的所有字符都為大寫。這個函數(shù)對符號和數(shù)字沒有任何作用,即,只是忽略了這些東西。
語法:string.upper()

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

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

輸出:
CSDN IS THE LARGEST DEVELOPER COMMUNITY IN CHINA

示例 2:如果有數(shù)字而不是字符會發(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ù)查找指定值(由用戶給定)在給定字符串中出現(xiàn)的次數(shù)。
語法: 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ù)在找不到值時引發(fā)異常。
語法: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 之間搜索時,字母“d”在文本中的哪個位置首次出現(xiàn)?

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

輸出:
20

示例 3:如果找不到該值,則 find() 函數(shù)返回 -1,但 index() 函數(shù)會引發(fā)異常

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

輸出:
-1

八、replace() 函數(shù)

replace() 函數(shù)用另一個指定的短語替換指定的短語。
注意:如果沒有指定任何其他內(nèi)容,所有出現(xiàn)的指定短語都將被替換。
語法: 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ù)返回一個字符串,其中所有大寫字母都是小寫字母,反之亦然。
語法:string.swapcase()
示例 1:將小寫字母變?yōu)榇髮?,將大寫字母變?yōu)樾?br/>

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

輸出:
csdn IS THE LARGEST DEVELOPER COMMUNITY IN cHINA

示例 2:如果有數(shù)字而不是字符會發(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ù)獲取可迭代對象中的所有項并將它們連接成一個字符串。我們必須指定一個字符串作為分隔符。
語法: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

到此這篇關于10個有用的Python字符串函數(shù)小結(jié)的文章就介紹到這了,更多相關Python字符串函數(shù)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!

國外服務器租用

版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

相關文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部