Python去除字符串中不需要的字符

Python去除字符串中不需要的字符

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

①ss.strip()参数为空时,默认去除ss字符串中头尾\r, \t, \n, 空格等字符;
②但是只能删除字符串中首尾的指定字符,中间的无法删除,需要删除中的字符,往下看。

ss = '\n\n我是我,\n你是你\n\n\n'
print(ss)
print("*"*15)
print(ss.strip('\n'))


运行结果如下:


我是我,
你是你



***************
我是我,
你是你

2.使用python内置的replace()函数

①表示代替的意思,同时也可进行字符串内指定字符的更换。
② replace(old, new[,max]),ol

你可能感兴趣的:(python)