婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 10個有用的Python字符串函數小結

10個有用的Python字符串函數小結

熱門標簽:百度地圖標注沒有了 ai電銷機器人源碼 西藏房產智能外呼系統要多少錢 地圖標注審核表 長沙高頻外呼系統原理是什么 宿遷星美防封電銷卡 外呼并發線路 ai電話機器人哪里好 湛江智能外呼系統廠家

前言

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

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

一、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

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

您可能感興趣的文章:
  • python入門字符串拼接\截取\轉數字理解學習
  • python入門課程第五講之序列和字符串
  • Python如何使用print()函數輸出格式化字符串
  • 怎么處理Python分割字符串時有多個分隔符
  • Python基本數據類型之字符串str
  • Python數字/字符串補零操作實例代碼
  • python如何正確的操作字符串
  • python字符串的多行輸出的實例詳解
  • Python的文本常量與字符串模板之string庫
  • 關于Python中字符串的各種操作

標簽:南平 漯河 大同 海南 林芝 普洱 盤錦 寧夏

巨人網絡通訊聲明:本文標題《10個有用的Python字符串函數小結》,本文關鍵詞  10個,有,用的,Python,字符串,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《10個有用的Python字符串函數小結》相關的同類信息!
  • 本頁收集關于10個有用的Python字符串函數小結的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 兰州市| 库尔勒市| 崇义县| 大石桥市| 石河子市| 柳河县| 井冈山市| 凤翔县| 龙陵县| 洪江市| 白水县| 霍山县| 巴塘县| 焦作市| 齐河县| 郴州市| 阿拉尔市| 黄大仙区| 洛阳市| 台山市| 西宁市| 饶河县| 郓城县| 积石山| 乌拉特中旗| 刚察县| 梅河口市| 阳信县| 镇江市| 福贡县| 焉耆| 吉木乃县| 阳新县| 班玛县| 甘孜| 苗栗市| 舟曲县| 游戏| 峨边| 罗源县| 凤山市|