如下是大部分Str的內置功能,我按照其源代碼的類定義說明,進行簡單的測試:
1.將首字母大寫,其他小寫。
name = "jAck"
result = name.capitalize()
print(result)
藍色底紋爲輸出結果:
# """
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# Jack
# """
2.字母轉小寫
name = "jAC1k"
result = name.casefold()
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# jac1k
# '''
3.居中補齊,可定義寬度和填充字符
center(self, width, fillchar=None)
name = 'hello'
result = name.center(51,"x")
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# xxxxxxxxxxxxxxxxxxxxxxxhelloxxxxxxxxxxxxxxxxxxxxxxx
# '''
4.計算出現次數,能夠設置搜索區間
#count(self, sub, start=None, end=None)
name = 'agabdfgroifgjingfdgsfi'
result = name.count('g',3,20)
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# 4
# '''
5.字符編碼轉換
#encode(self, encoding='utf-8', errors='strict')
name = 'abc'
result = name.encode("gbk")
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# b'abc''''
6.判斷是否以**字符(串)結尾
#endswith(self, suffix, start=None, end=None)
name = 'dfdfadfkl'
result = name.endswith('fk',3,len(name)-1)
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# True
# '''
7.製表符轉空格,能夠指定每一個製表符寬度
#expandtabs(self, tabsize=8)
name = "i\tlove\tyou"
print(name)
result = name.expandtabs(1)
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# i love you
# i love you
# '''
8.尋找是否存在,返回位置或者-1
find(self, sub, start=None, end=None)
name = "123456795"
result1 = name.find("5")
result2 = name.find("8")
print (result1,"\n",result2)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# 4
# -1
# '''
9.字符串格式化,能夠按順序{},或者變量替換
#format(self, *args, **kwargs)
name = "a {1} jack {0} {a}"
result = name.format("little","big",a="333!!")
print (result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# a big jack little 333!!
# '''
10.返回位置
name = "012345678"
result = name.index("4" ,3,8)
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# 4
# '''
11.判斷是不是數字或字母
name = '123a'
result = name.isalnum()
print (result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# True
# '''
另外的,.isalpha()判斷字母 , .isdecimal() 判斷十進制,.isdigit()判斷數字 ,.isidentifier()判斷標識 ,islower()判斷小寫, ..isnumeric()判斷數值,.isprintable()判斷打印
isspace()判斷空格,istitle()判斷標題,isupper()判斷大寫 等,都大同小異
12.字符拼接,能夠填充
name = "hello"
result = name.join("123a")
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# 1hello2hello3helloa
# '''
13.向左填充和向右填充
name = 'lucky!'
result = name.ljust(25,"+")
result1 = name.rjust(25,"=")
print(result,'\n',result1)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# lucky!+++++++++++++++++++
# ===================lucky!
# '''
14.大寫小寫轉換
name = 'aBddKk'
result1 = name.lower()
result2 = name.upper()
print(result1,result2)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# abddkk ABDDKK
# '''
15.空格移除
name = ' I love you! '
result1 = name.strip()
result2 = name.lstrip()
result3 = name.rstrip()
print(name,'\n',result1,'\n',result2,'\n',result3)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# I love you!
# I love you!
# I love you!
# I love you!
# '''
16.定製批量替換
intab = "123"
outtab = "abc"
a="".maketrans(intab,outtab)
b = "1234567==="
result = b.translate(a)
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# abc4567===
# '''
17.分割(有保留)
partition(self, sep)
S.partition(sep) -> (head, sep, tail)
name = 'abcdefg'
result = name.partition("cd")
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# ('ab', 'cd', 'efg')
# '''
18.替換
name = "1234544555"
result = name.replace('4','H',1)
result1 = name.replace("4","H")
print(result)
print(result1)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# 123H544555
# 123H5HH555
# '''
19.分割
name = "123445334555"
result = name.split("3")
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# ['12', '445', '', '4555']
# '''
20.行分割
name='''
aa
bb
cc
dd'''
result = name.splitlines( )
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# ['', 'aa', 'bb', 'cc', 'dd']
# '''
21.大寫變小寫,小寫變大寫
name='SdfergL'
result = name.swapcase()
print(result)
# '''
# E:\Python3\python3.exe E:/pycharm/s12/work2/str_text.py
# sDFERGl
# '''