1. 程式人生 > 實用技巧 >Python程式設計整理:字串中常用的操作

Python程式設計整理:字串中常用的操作

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