Why the output is not getting striped of 'The'

跟風遠走 提交于 2020-06-01 07:11:10

问题


I want to strip 'The' from the string using strip function of string only no replace function should be used and can i know why three single quotes?

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)

I expect the output without the in the starting but it is not getting striped


回答1:


You are doing right, but as there is leading space before The, it is not striping as you expected. By default, strip will remove all leading and trailing spaces.

So, you can try strip in this way,

>>> zenPython.strip().strip('The')

Output:

" 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!"



回答2:


Your input string actually starts with a space. In this case, you might want to consider using re.sub here:

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

This removes an initial word The at the start of the input, possibly preceded by any amount of whitespace, which would also be removed.




回答3:


.strip() is not the right function to use for what you want to achieve. It happens to have the right result (for this specific string), but for wrong reasons. It will delete all characters from beginning and end, matching the collection provided. That means:

"ThehehxxehT".strip("The") == "xx"

For a better solution, use the re.sub mentioned in another comment or strip it manually by length:

def remove_prefix(original, prefix):
  if original.startswith(prefix):
    return original[len(prefix):]
  else:
    return original

zen = remove_prefix(zen, "The")

In your case you'll also have to remove the initial newline in the string (your string starts with <newline>The).

The triple quotes are to allow a multi-line string.



来源:https://stackoverflow.com/questions/57765659/why-the-output-is-not-getting-striped-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!