Python編程整理:字串中常用的操作
1. 切片
msg[(起始位置):(結束位置):(步長)]
顧頭不顧尾
步長默認為1,并且可以是負數,表示反向取
例子:
msg = "hello world" print(msg[0:2]) # he
msg = "hello world" print(msg[::-1]) # dlrow olleh
[::-1]為常用的逆序輸出
2. 成員運算in以及not in
msg = "a b c d e f" print('a' in msg) # True
msg = "a b c d e f" print('a' not in msg) # False
3. 移除字串左右兩邊的字符strip
移除空格的用法:
msg = ' content ' print(msg.strip()) # content
移除兩邊的字符:
msg = ' **! content !** ' print(msg.strip('*! ')) # content
在strip中寫入所有想去除的字符,字串兩段的這些字符就會被去除
另外,還有去除單邊字符的函式lstrip和rstrip,例子:
msg = ' content ' print(msg.lstrip()) print(msg.rstrip()) # content # content
4. 替換字符函式replace
msg = 'abababab' print(msg.replace('ab', 'AB')) # ABABABAB
將給定的字符替換為其他字符,
同時,replace還能加第三個引數來規定替換的次數,比如:
msg = 'abababab' print(msg.replace('ab', 'AB', 2)) # ABABabab
5. 切分操作split
msg = '1|2|3|4' list1 = msg.split('|') print(list1) # ['1', '2', '3', '4']
split中還可以添加一個正整數引數,代表切分次數,比如:
msg = '1|2|3|4' list1 = msg.split('|', 2) print(list1) # ['1', '2', '3|4']
另外,rsplit函式時從右往左切割,在不指定次數的情況下,結果個split相同,
msg = '1|2|3|4' list1 = msg.rsplit('|', 2) print(list1) # ['1|2', '3', '4']
6.連接操作join
可以理解為split的逆向操作
list1 = ['1', '2', '3', '5'] print(':'.join(list1)) # 1:2:3:5
這邊要注意的是,串列中的元素必須全部都是字串型別才能使用join函式來連接
7.判斷字串是否為純數字isdigit
msg1 = 'abc123' msg2 = '123456' print(msg1.isdigit()) print(msg2.isdigit()) # False # True
8.格式化字符輸出.format方法(最常用)
a = 'hello' b = 'world' print('{} {}'.format(a ,b)) # hello world
重復字符使用如下:
a = 'hello' b = 'world' print('{0} {0} {0} {1} {1}'.format(a, b)) # hello hello hello world world
也可以用臨時變數來實作重復:
a = 'hello' b = 'world' print('{text1} {text1} {text2} {text2} {text2}'.format(text1 = a, text2 = b)) # hello hello world world world
還有個簡便用法,只限定python3.5之后:(暫時不建議使用)
a = 'hello' b = 'world' print(f'{a} {b} {b}') # hello world world
9. 尋找操作 find
msg = 'hello world' print(msg.find(' ')) # 5
空格第一次出現在第5個字符,因此輸出的是數字5
rfind函式則是從左往右找,例如:
msg = 'hello world' print(msg.find('l')) print(msg.rfind('l')) # 2 # 9
find找到的‘l’字符是在第2個位置上的
rfind找到的‘l’字符實在第9個位置上的
找不到會回傳-1:
msg = 'hello world' print(msg.find('a')) # -1
find還能添加兩個引數分別表示起始位置和結束位置:
msg = 'hello world' print(msg.find('l', 7, 10)) # 9
count函式統計出現字符的次數:
msg = 'hello world' print(msg.count('l')) # 3
10. 文本列印操作
center居中操作:
msg = 'hello world' print(msg.center(50,'*')) # *******************hello world********************
center第一個引數為寬度設定,第二個字符引數為填充字符
ljust居左顯示與rjust居右顯示:
msg = 'hello world' print(msg.ljust(50, '*')) print(msg.rjust(50, '*')) # hello world*************************************** # ***************************************hello world
零填充函式zfill,默認從右對齊,在規定長度下用0填充,比如:
num = '111' print(num.zfill(10)) # 0000000111
11. 制表符操作
expandtabs可以改變指標符\t的空格數量
比如:
print('hello\tworld') print('hello\tworld'.expandtabs(1)) # hello world # hello world
制表符默認空格數為4
12.英文字母大小寫操作
msg = 'hello world' print(msg.upper()) print(msg.lower()) print(msg.capitalize()) print(msg.swapcase()) # 大寫變小寫 小寫變大寫 print(msg.title()) # HELLO WORLD # hello world # Hello world # HELLO WORLD # Hello World
13.識別數字
isdigit可以識別純數字,而isnumeric函式可以識別數字相關的文字,比如:
num = '四' print(num.isnumeric()) # True
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/75654.html
標籤:Python
上一篇:Python代碼注釋的用法和意義
下一篇:ATM購物車(一)
