python - 为什么输出没有被删除'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)
我希望在开始时没有输出,但它没有条纹
解决方案
您的输入字符串实际上以空格开头。在这种情况下,您可能需要考虑re.sub
在此处使用:
zen = re.sub(r'^\s*The\b', zenPython)
这将删除输入开头的初始单词The
,可能前面有任意数量的空格,这些空格也将被删除。
推荐阅读
- r - 使用 Shiny 时自动更新 DT 有效,但在具有多个选项卡的 Shinydashboard 中无效
- tfs-workitem - 工作项模板的 Azure DevOps Services 权限
- java - 在 Visual Studio Code 中获得与 PowerShell 或 CMD 相同的 JAVA 编译错误
- typescript - TypeScript 类型上不存在属性“加载”?
- powershell - Powershell命令根据过滤器从多个文件夹中获取所有文件
- python - 测试用例使 os.getuid() 和 os.geteuid() 返回不同的结果
- matlab - 查找与“knnsearch”matlab函数的一一对应关系
- postgresql - PostgreSQL 中内联 SQL 函数调度的解决方法
- python - 如何在任何条件下通过另一个值设置熊猫值
- c# - .Net Core 3 WebAPI 在随机使用几天后变得无响应