描述
python strip() ,用於去除述字符串頭尾指定字符(默認為空格或換行符)或字符序列。
注意:此方法只能去除頭尾的空格或是換行符,不能去除中間的。
語法:
str.strip([chars])
參數:
chars -- 移除字符串頭尾的指定字符序列
返回值:
返回字符串中新生成的序列
實例
1 # !/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 str = "00000003210python01230000000"; 5 print(str.strip('0')) # 去除首尾字符 0 6 7 str2 = " python " # 去除首尾空格 8 print(str2.strip()) 9 10 str3 = "123python321" # 去除12 11 print(str3.strip('12'))
打印結果:
1 3210python0123 2 python 3 3python3