首页 > 解决方案 > 为什么输出没有被删除'The'

问题描述

我想使用字符串的strip函数从字符串中去除'The',不应该使用replace函数,我能知道为什么三个单引号吗?

zenPython = '''
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''

zen=zenPython.strip('The')

print(zen)

我希望在开始时没有输出,但它没有条纹

标签: pythonstringstrip

解决方案


您的输入字符串实际上以空格开头。在这种情况下,您可能需要考虑re.sub在此处使用:

zen = re.sub(r'^\s*The\b', zenPython)

这将删除输入开头的初始单词The,可能前面有任意数量的空格,这些空格也将被删除。


推荐阅读