No1.字符串的魔法

#首字母大寫
>>> test = "trony"
>>> v = test.capitalize()
>>> print(v)
Trony
#將字符串中大寫變小寫有兩個方法casefold()和lower(),casefold()更加的牛逼,它可以將許多未知的字符串變小寫
>>> test = "FDKFJ"
>>> v1 = test.casefold()
>>> print(v1)
fdkfj
>>> v2 = test.lower()
>>> print(v2)
fdkfj
#center()方法設置寬度並將內容居中
>>> test = "trony"
>>> v = test.center(20, "*")
>>> print(v)
*******trony********
#count()計算當前當前字符列的出現次數,後面參數可以指定範圍
>>> test = "tronytrony"
>>> v = test.count('o')
>>> print(v)
2
>>> v1 = test.count("o", 5, 6)
>>> print(v1)
0
#endswith()以什麼結尾,startswith()以什麼開始,可指定範圍
>>> test = "trony"
>>> v = test.endswith('a')
>>> print(v)
False
>>> v1 = test.startswith("t")
>>> print(v1)
True
#從開始往後找,找到第一個之後,獲取其位置,可指定範圍
>>> test = "pythonpython"
>>> v = test.find("on")
>>> print(v)
4
#字符串的格式化,將字符串中的佔位符替換爲指定的值
>>> test = "i am {name}, age {a}"
>>> v = test.format(name = "trony", a = 19)
>>> print(v)
i am trony, age 19
>>> test1 = "i am {0}, age {1}"
>>> v1 = test1.format("trony", 19)
>>> print(v1)
i am trony, age 19
>>> test2 = "i am {name}, age {a}"
>>> v2 = test2.format_map({"name":"trony", "a":19})#與format()類似不過,format_map()傳入的是字典
>>> print(v2)
i am trony, age 19
#尋找字符串,並返回其下標,找不到直接報錯
>>> test = "tronytrony"
>>> v = test.index("8")
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    v = test.index("8")
ValueError: substring not found
>>> v1 = test.index("o")
>>> print(v1)
2
#判斷該字符串是否只有數字和字母
>>> test = "dsf465"
>>> v = test.isalnum()
>>> print(v)
True
#expandtabs(value)遇到製表符則,以value作爲位數開始數,遇到製表符製表符表示剩下的位數
>>> test = "sfsf\tjdfkd\t"
>>> v = test.expandtabs(6)
>>> print(v)
sfsf  jdfkd 
#判斷是否只有字母
>>> test = "是trony"
>>> v = test.isalpha()
>>> print(v)
True
>>> 
#判斷是否是數字,厲害程度isdecimal()<isdigit()<isnumeric()
>>> test = "123"
>>> v1 = test.isdecimal()
>>> v2 = test.isdigit()
>>> v3 = test.isnumeric()
>>> print(v1, v2, v3)
True True True
#標識符:數字、字母、下劃線。isidentifier()判斷是否爲標識符
>>> a = "123546"
>>> v = a.isidentifier()
>>> print(v)
False
>>> v1 = b.isidentifier()
>>> print(v1)
True
#判斷字符串是否包含不可顯示的字符如:\t、\n
>>> test = "sfsj\n"
>>> v = test.isprintable()
>>> print(v)
False
>>> test2 = "sdfjkds"
>>> v2 = test2.isprintable()
>>> print(v2)
True
#判斷字符串是否是空格
>>> test = "dfdkf"
>>> v = test.isspace()
>>> print(v)
False
>>> test1 = "djfk   "
>>> v1 = test1.isspace()
>>> print(v1)
False
>>> test2 = "    "
>>> v2 = test2.isspace()
>>> print(v2)
True
#istitle()判斷字符串是否是標題,title()將字符串轉化爲標題
>>> test = "trony tiger"
>>> v1 = test.istitle()
>>> print(v1)
False
>>> v2 = test.title()
>>> print(v2)
Trony Tiger
>>> v3 = test.istitle()
>>> print(v3)
False
#將字符串中的每一個元素按照指定的分隔符連接起來
>>> li = ['fd', 'dfd', 'dfdf']
>>> v1 = "_".join(li)
>>> print(v1)
fd_dfd_dfdf
>>> tu = ('fdf', 'fd')
>>> v2 = "_".join(tu)
>>> print(v2)
fdf_fd
#類似於center()
>>> test = "trony"
>>> v = test.ljust(20, "*")
>>> print(v)
trony***************
>>> v1 = test.rjust(20, "*")
>>> print(v1)
***************trony
#islower()與lower()
#isupper()與upper()
>>> test = "Trony"
>>> v = test.islower()
>>> print(v)
False
>>> v1 = test.lower()
>>> print(v1)
trony
>>> v2 = test.isupper()
>>> print(v2)
False
>>> v3 = test.upper()
>>> print(v3)
TRONY
#默認去除兩端空白,也可以去處\t、\n
#傳入參數時,按照最大匹配原則去除字符串
>>> test = "  trony  "
>>> v1 = test.strip()
>>> v2 = test.lstrip()
>>> v3 = test.rstrip()
>>> print(v1, v2, v3)
trony trony     trony
#使用maketrans()方法構造一個對應關係,使用translate()方法進行替換
>>> v = "fdkfladjafkljaifoagngk"
>>> m = str.maketrans("aeiou", "12345")
>>> new_v = v.translate(m)
>>> print(new_v)
fdkfl1dj1fklj13f41gngk
#分割,使用partitionn()分割以value進行分割可以獲得三部分,只能接收一個參數
#使用split()分割以value進行分割,可以接收兩個參數
>>> test = "fdfsfdssdgsfbdhsfdgw"
>>> v = test.partition('s')
>>> print(v)
('fdf', 's', 'fdssdgsfbdhsfdgw')
>>> v = test.rpartition('s')
>>> print(v)
('fdfsfdssdgsfbdh', 's', 'fdgw')
>>> v3 = test.split('s', 3)
>>> print(v3)
['fdf', 'fd', '', 'dgsfbdhsfdgw']
>>> v4 = test.rsplit('s')
>>> print(v4)
['fdf', 'fd', '', 'dg', 'fbdh', 'fdgw']
#splitlines()只能以換行進行分割,可以設置True或False來確認是否保留換行符
>>> test1 = "dsfs\nfdfdf\n"
>>> v5 = test1.splitlines(True)
>>> print(v5)
['dsfs\n', 'fdfdf\n']
>>> v6 = test1.splitlines(False)
>>> print(v6)
['dsfs', 'fdfdf']
#大小寫轉換
>>> test = "dljkdfjJJLKFD"
>>> v = test.swapcase()
>>> print(v)
DLJKDFJjjlkfd
#替換,可以接收三個參數,前兩個爲替換內容,最後一個爲替換次數
>>> test = "tronytronytrony"
>>> v = test.replace("tr", "bbb", 3)
>>> print(v)
bbbonybbbonybbbony























發佈了33 篇原創文章 · 獲贊 0 · 訪問量 3111
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章