我喜欢string.capwords()
的一些行为方式,也喜欢.title()
的一些行为方式,但不是一种。在
我需要缩写字母大写,.title()
可以,但是string.capwords()
不能,而且{}不在单引号后大写字母,所以我需要两者的组合。我想使用.title(),
,然后我需要在撇号后面的单个字母小写,前提是它们之间没有空格。在
例如,下面是一个用户的输入:
string="it's e.t.!"
我想把它转换成:
^{pr2}$
.title()
将大写“s”,而string.capwords()
不会将“e.t.”大写。在
Tags:
可以使用正则表达式替换(请参见^{} ):
[a-z]
将匹配小写字母。但不是在'
((?<!')
-否定查找后断言)之后。并且字母应该出现在单词边界之后;因此t
将不匹配。在re.sub
,lambda
的第二个参数将返回替换字符串。(信的上部版本)并将用于替换。在如果您不想使用regex,您可以始终使用这个简单的for循环
希望这有帮助:)
编辑后改为使用大写函数。现在它开始看起来像有用的东西了:)。但是这个解决方案不适用于其他空白字符。为此,我会同意费斯托鲁的解决方案。在
相关问题 更多 >
编程相关推荐