Python去除空白符号\s

2024-07-01 07:55:54 发布

您现在位置:Python中文网/ 问答频道 /正文

例如我有这个地址\sgoogle.com网站我用

line.strip(' \s') # it displays google.com as it must be

但当我在\sgoogle\s.com上尝试时,效果很差(结果是谷歌公司)。有人知道哪里出了问题,怎么解决?在


Tags: com网站地址asgoogleline公司it
2条回答

strip()只删除字符串开头和结尾的字符,而不是字符串中间的字符。如果要删除字符串中的任何位置,请使用replace

'\sgoogle\s.com'.replace('\s', '');

我想我可能会包括一个替代方案(仅限于Python 2):

>>> s = '\sgoogle\s.com'
>>> s.translate(None, '\s')
'google.com'

在Python 3中,它将是:

^{pr2}$

相关问题 更多 >

    热门问题