2024-03-29 14:47:52 发布
网友
假设我们希望我们的最终输出为roll-tide:
alabama = "roll\ tide"
这不起作用,因为我们需要转义:
所以我用了'\\' as instructed,但它仍然这样做:
>>>print(laura.strip('\\')) roll\ tide
试着替换它:
print(alabama.replace('\\',''))
我想你需要分开:
>>> alabama.split("\\") ['roll', ' tide'] >>> "".join(alabama.split("\\")) 'roll tide'
strip只从字符串的前面或结尾删除
strip
如果“\”在结尾:
您也可以使用re.sub:
re.sub
>>> import re >>> re.sub(r"\\","",alabama) 'roll tide'
您也可以使用str.replace:
str.replace
>>> alabama.replace("\\","") 'roll tide'
str.strip只在字符串的开头和结尾处起作用。在
str.strip
alabama = "\\roll\\ tide\\" alamaba.strip("\\") # "roll\\ tide"
而是使用str.replace并将其替换为空。在
试着替换它:
我想你需要分开:
strip
只从字符串的前面或结尾删除如果“\”在结尾:
^{pr2}$您也可以使用
re.sub
:您也可以使用
str.replace
:str.strip
只在字符串的开头和结尾处起作用。在而是使用
^{pr2}$str.replace
并将其替换为空。在相关问题 更多 >
编程相关推荐