python中的strip()可以去除头尾指定字符

print(ss.strip('\n'))

结果:

可以看到只能删除头尾指定字符。

想要去除中间字符,可以使用replace()函数

print(ss.replace('\n', ''))

结果:

note:

1. strip(str)

基本用法:ss.strip(rmStr)

ss.strip()参数为空时,默认去除ss字符串中头尾\r, \t, \n, 空格等字符

ss.lstrip()删除ss字符串开头处的指定字符,ss.rstrip()删除ss结尾处的指定字符

2. replace(old, new[, max])

基本用法:ss.replace(rgExp, replaceText, max)

rgExp是指正则表达式模式或可用标志的正则表达式对象,也可以是 String 对象或文字;replaceText是一个String 对象或字符串文字;max是一个数字。对于一个对象,在对象的每个rgExp都替换成replaceText,从左到右最多max次。