1.分隔符split
str = “abc abcd 123”
x= str.split()
print (x)
[‘abc’, ‘abcd’, ‘123’]
txt = “Google#Runoob#Taobao#Facebook”
#第二个参数为1,返回两个参数列表
x = txt.split("#", 1)
print (x)
[‘Google’, ‘Runoob#Taobao#Facebook’]
txt = “Google#Runoob#Taobao#Facebook”
#第二个参数为3,返回四个参数列表
x = txt.split("#", 3)
print (x)
[‘Google’, ‘Runoob’, ‘Taobao’, ‘Facebook’]
##########################################################
strip()
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
str = “00000003210Runoob01230000000”;
x = str.strip(‘0’) # 去除首尾字符 0
print (x)
3210Runoob0123
str = " ethan "; # 去除首尾空格
x= str.strip()
print (x)
ethan
##########################################################
%s%s %d%d 字符替换
print ( ‘%s%d’ %(‘abc’,5))
abc5
print ( ‘%6s%2d’ %(‘abc’,5)) #正数表示从左开始
abc 5
print ( ‘%-6s%2d’ %(‘abc’,5)) #负数表示从右开始
abc 5