str.strip(rm) : 刪除s字符串中開頭、結尾處,位於 rm刪除序列的字符
str.lstrip(rm) : 刪除s字符串中開頭(左邊)處,位於 rm刪除序列的字符
str.rstrip(rm) : 刪除s字符串中結尾(右邊)處,位於 rm刪除序列的字符
str.replace(‘s1’,’s2’) : 把字符串裏的s1替換成s2。故可以用replace(’ ‘,”)來去掉字符串裏的所有空格
str.split() : 通過指定分隔符對字符串進行切分,切分為列表的形式。
去除兩邊空格:
>>> str = ' hello world '
>>> str.strip()
'hello world'
1
2
3
1
2
3
去除開頭空格:
>>> str.lstrip()
'hello world '
1
2
1
2
去除結尾空格:
>>> str.rstrip()
' hello world'
1
2
1
2
去除全部空格:
>>> str.replace(' ','')
'helloworld'
1
2
1
2
將字符串以空格分開:
>>> str.split()
['hello', 'world']
>>>