python - Python text.replace 用于删除字符串的一部分
问题描述
我想看看如何在不留空白的情况下删除字符串的某个部分。
运行此代码时,它输出nice today
,问题是我用空格替换字符。有没有解决这个问题或者我应该完全使用完全不同的方法?
text = ("hello bye there ")
print(text.replace("hello", "nice").replace("there","today").replace("bye", ""))
解决方案
你应该首先删除空格:
text = ("hello bye there ")
print(text.replace("hello", "nice").replace("there","today").replace(" ", '').replace("bye" , " "))
输出将是:
nice today
推荐阅读
- c# - 如何根据列表中的其他属性对列表进行排序
- matlab - MATLAB Mex C API 检查 mxarray 是否为基类
- reactjs - ReactJS,componentWillReceiveProps可以获取数据,但是render不能
- laravel - carregar múltiplos modelos em um 控制器
- actions-on-google - 智能家居 - 正确的 JSON 以在设备离线时响应 EXECUTE
- concourse - 如何测试大厅自定义资源类型?
- java - 我需要一些关于带有 hashmap 的用户类型的建议
- docker - 如何使用 docker-compose 从多个私有注册表中提取?
- c++ - 表达式结果与表达式所表示的内容之间的差异
- python - python 两个列表对象没有像往常一样