用最恐怖的方式替换第一个和最后一个字符串
发布时间:2020-12-30 10:53:55 所属栏目:Python 来源:互联网
导读:我正在寻找用于替换字符串的第一个和最后一个字的最 pythonic方式(在字母的基础上执行它不会因各种原因而起作用).为了演示我正在尝试做什么,这是一个例子. a = this is the demonstration sentence. 我希望我的python函数的结果是: b = This is the demonstr
|
我正在寻找用于替换字符串的第一个和最后一个字的最 pythonic方式(在字母的基础上执行它不会因各种原因而起作用).为了演示我正在尝试做什么,这是一个例子. a = "this is the demonstration sentence." 我希望我的python函数的结果是: b = "This is the demonstration Sentence." 其中棘手的部分是字符串的前端或末端可能有空格.我需要保留这些. 这就是我的意思: a = " this is a demonstration sentence. " 结果需要是: b = " This is a demonstration Sentence. " 也会对正则表达式是否比python的内置方法更好地完成这项工作的意见感兴趣,反之亦然. 解决方法import re
a = " this is a demonstration sentence. "
print(re.sub(r'''(?x) # VERBOSE mode
( #
^ # start of string
s* # zero-or-more whitespaces
w # followed by an alphanumeric character
)
| # OR
(
w # an alphanumeric character
S* # zero-or-more non-space characters
s* # zero-or-more whitespaces
$ # end of string
)
''',lambda m: m.group().title(),a))
产量 This is a demonstration Sentence. (编辑:吉安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – 为什么使用整数作为pymongo的键不起作用?
- python – 在大熊猫中合并多索引的单索引数据帧
- python – 将几个正则表达式合并到一个RE中
- version-control – 从hg存储库中删除二进制文件
- `with canvas:`(Python`with something()as x:`)如何隐式
- python – 改变Kivy中Button的背景颜色
- python – 为Django应用程序中的用户添加动作的日志条目
- python – 我应该使用GeoDjango来映射平面图吗?
- python – 如何在`scipy.integrate.dblquad`中增加函数的细
- python – 如何使员工可以访问Django设置?
推荐文章
站长推荐
- 为什么以下示例中的python广播比简单循环慢?
- 将电子表格的列存储在Python字典中
- Python-redis keys()返回字节对象列表而不是字符
- 如何使用PIL(python-imaging)创建透明的gif(或pn
- Python – Multiprocessing.processes从可执行文
- python – 什么是django.utils.functional .__ p
- python – 在Pandas中复杂(对我而言)从宽到长重塑
- 如何使用Python的ctypes和readinto读取包含数组的
- python – plotly.offline.iplot给出一个大的空白
- Python线程与事件对象
热点阅读
