python字符串函數總結


字符串函數主要分為:

查找:

 1 strs = "this is a line of text for test"
 2 
 3 index1 = strs.find("is")
 4 print(index1)
 5 # 指定范圍,指定起始結束位置
 6 index2 = strs.find("is", 0, 15)
 7 print(index2)
 8 # find 查找不存在,返回-1,返回第一個字符索引值
 9 print(strs.find("isa"))
10 # rfind,從右邊開始查找
11 print(strs.rfind("is"))
12 # index 查找,不存在,會報錯
13 print(strs.index("test"))
14 # 可以指定起始,結束位置,不包括結束位置
15 # print(strs.index("test",0,15))
16 # rindex(),從右邊開始查找
17 print(strs.rindex("is"))
18 # count(str,start,end)返回str在start,end出現的次數,不包括結束位置
19 print(strs.count("is", 0, 4))
20 print(strs.count("is"))

 替換

# replace(old,new,count)
# 把字符串中的old替換成new,count指定替換的最大次數,替換次數<=count,不指定默認替換所有
# 返回替換后的字符串
print(strs.replace("is", "SS", 4))
print(strs.replace("is", "SS"))
print(strs)

字母處理(大小寫轉換):

 1 # capitalize(),將字符串第一個字符大寫,返回替換后新的字符串
 2 str2 = "this is a test for a line text"
 3 str3 = str2.capitalize()
 4 print(str3)
 5 # title(),將字符串的每個單詞首字母大寫,返回替換后新的字符串
 6 str4 = str2.title()
 7 print(str4)
 8 # lower(),將字符串的大寫字母替換成小寫字母,返回
 9 str5 = "THIS IS A TEST TO A LINE OF text"
10 str6 = str5.lower()
11 print(str6)
12 # upper() ,將字符串的小寫字母替換成大寫字母,返回
13 print(str6.upper())
14 # swapcase()    # 大小寫互換
15 str6="This IS A Test"
16 print("str6",str6.swapcase())

字符串格式化對齊相關:

#ljust(width,fillstr),左對齊
#rjust(width,fillstr),右對齊
#center(width,fillstr),居中
#默認用空格填充指定寬度,可以指定字符串,返回填充后的字符串
print("this is a title".center(52,"-"))
print("|","|".rjust(51))
print("|","this is text".center(51,"*"),"|",sep="")
print("|".ljust(51," "),"|")
print("-"*53)
字符串去空格及去指定字符
# strip(),刪除字符串兩端的空白字符,可以指定刪除的字符
# lstrip(),刪除字符串左邊的空白字符,可以指定刪除的字符
# rstrip()#刪除字符串末尾的空白字符,可以指定刪除的字符
str7 = " this is a test for blank "
print(str7)
print(str7.strip())
print(str7.lstrip())
print(str7.rstrip())

字符串切割

# split(sp,maxsplit) ,把字符串以sp切割符(默認空格)切割,maxsplit指定切割次數,
# 返回切割后字符串,組成的列表
str1 = "張三|23|180|58"
fields = str1.split("|")
fields = str1.split("|", 2)
print(fields)
str8="""
我是第一個行
我是第二行
我是第三行
我是第四行
我是第五行
"""
# splitlines(keepends) ,按照行分隔,返回一個包含各行作為元素的列表
# keepends,True顯示\n,False不顯示\n
lines = str8.splitlines(True)
for i in lines:
    print(i)

partition() 分區

  • str1.partition("sep")
  • 以sep拆分,放到元組中
  1. sep第一次出現時分割,返回一個包含分割符前部分、分割符本身和分割符后部分三部分組成的tuple

如:

str1 = "abccdc"
print(str1.partition("c"))

運行結果:

('ab', 'c', 'cdc')

字符串判斷相關

  • (4)islower():判斷字符串是否以純小寫字母組成

  • (5)isupper():判斷字符串是否以純大寫字母組成

  • (6)isalpha():判斷字符串是否全為字母(漢字屬於字母范圍)

  • (7)isalnum():判斷字符串是否全為字母和數字

  • (8)isspace():判斷字符串是否全為空格

  • (9)istitle():判斷字符串是否為標題格式(每個單詞首字母大寫)

# startswith(),檢查字符串是否是以指定字符串開頭,是,返回True,否則,返回False
# 可以加指定起始結束位置,
str2 = "this is a test for a line text"
bo = str2.startswith('th')
print(str2.startswith('this', 0, 3))
print(bo)

# endswith(suffix,start,end),檢查字符串是否是以指定字符串開頭,是,返回True,否則,返回False
# 可以加指定起始結束位置,
bo1 = str2.endswith("text")
print(bo1)

# isdigit()
# True: Unicode數字,byte數字(單字節),全角數字(雙字節),羅馬數字
# False: 漢字數字
# Error: 無
#
# isdecimal()
# True: Unicode數字,,全角數字(雙字節)
# False: 羅馬數字,漢字數字
# Error: byte數字(單字節)
#
# isnumeric()
# True: Unicode數字,全角數字(雙字節),羅馬數字,漢字數字
# False: 無
# Error: byte數字(單字節)


#isalpha(),如果字符串中所有字符都是字母 則返回 True,否則返回 False
#當字符串設置為utf-8,不能檢測中文
str9 = "ia ma litttl ejoy"
print(str9.isalpha())# False,包含空格
str9 = "hahhayoudontseemewhatimeans"
print(str9.isalpha())#True
# isdigit(),如果字符串中只包含數字則返回 True 否則返回 False.
str10 = "123456789043"
print(str10.isdigit())#True
str10 = "1234567a89043"
print(str10.isdigit())#False
str10 = "1234 56789043 "
print(str10.isdigit())#False
#isalnum(),如果字符串中所有字符都是字母(中文)或數字則返回 True,否則返回 False
print("----"*39)
str11 = "1234567a89043"
str11 = u"1234567a89043咋呼大".encode("utf-8")
# isnumeric() 檢測字符串中的數字,純數字(中文的數字(一,壹,二貳),羅馬數字ⅡⅢⅣⅤ)返回True,
# str11 = '12233一二三叄④'
# print(str11.isnumeric())
print(str11.isalnum())
print(str11.isalpha())
print("一壹二貳ⅡⅢⅣⅤ₃⅔❽⒇".isnumeric())
#isspace(), 如果字符串中只包含空格,則返回 True,否則返回 False.
# print("\t\n  ".isspace()) True

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM