python字符串
语法查阅链接:python语法查阅
1.Python strip()方法
- 描述:Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
- 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
- 语法
str.strip([chars])
- 代码
str.strip('\n')#移除首尾换行符
str.strip(' ')#移除首尾空格
str.strip('0')#移除首尾字符0
str.strip('123')#移除首尾字符123
str.strip('\n').strip(' ')#同时移除换行符和空格
2.Python replace()方法
- 描述:replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
- 语法:
str.replace(old, new[, max])
- 参数
old – 将被替换的子字符串。
new – 新字符串,用于替换old子字符串。
max – 可选字符串, 替换不超过 max 次 - 返回值
返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。 - 代码
str.replace('\n','').replace(' ','').replace('(','').replace(')','')
str.replace(',',','))