为什么输出没有被“The"条带化 [英] Why the output is not getting striped of 'The'

查看:42
本文介绍了为什么输出没有被“The"条带化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

zenPython = '''Python 之禅,作者 Tim Peters美丽总比丑陋好.显式优于隐式.简单胜于复杂.复杂总比复杂好.扁平比嵌套好.稀疏比密集好.可读性很重要.特殊情况不足以打破规则.虽然实用性胜过纯度.错误永远不应该静默传递.除非明确沉默.面对模棱两可,拒绝猜测的诱惑.应该有一种——最好只有一种——明显的方法来做到这一点.尽管这种方式起初可能并不明显,除非您是荷兰人.现在总比没有好.尽管现在永远不会比*正确*更好.如果实现很难解释,那是个坏主意.如果实现很容易解释,这可能是一个好主意.命名空间是一个很棒的想法——让我们做更多的事情!'''zen=zenPython.strip('The')打印(禅)

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

解决方案

你做对了,但是由于 The 前有前导空格,所以不是 striping正如你所料.默认情况下,strip 将删除所有前导和尾随空格.

所以,你可以试试这样strip,

<预><代码>>>>zenPython.strip().strip('The')

<小时>

输出:

" Python 之禅,作者 Tim Peters 美丽胜于丑陋.显式胜于隐式.简单胜于复杂.复杂胜于复杂.扁平胜于嵌套.稀疏胜于密集.可读性很重要.特殊情况不足以打破规则.虽然实用性胜过纯粹.错误永远不应该默默地传递.除非明确地沉默.面对歧义,拒绝猜测的诱惑.应该有一个——最好是只有一种--明显的方法.尽管除非您是荷兰人,否则这种方式一开始可能并不明显.现在总比没有好.虽然从不经常比现在好.如果实现很难解释,这是一个坏主意.如果实现很容易解释,那可能是个好主意.命名空间是一个很棒的主意——让我们做更多的事情!"

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

解决方案

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

这篇关于为什么输出没有被“The"条带化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆