这甚至将撇号后的字母大写,这不是预期的结果,
shared_ptr
有些人建议使用capwords,但是capwords似乎已经瘫痪了(只有
大写单词前面加上空格。在这种情况下,我还需要能够将由句点分隔的单词大写(例如:one.two.three应该在One.Two.Three上生成)。
是否有一种方法在capwords和title没有失败的地方?
答案 0 :(得分:3)
在python的文档here中找到了解决您确切问题的方法:
>>>
>>> import re
>>> def titlecase(s):
... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
... lambda mo: mo.group(0)[0].upper() +
... mo.group(0)[1:].lower(),
... s)
...
答案 1 :(得分:1)
import string
string.capwords("there's a need to capitalize every word")
答案 2 :(得分:0)
您可以在re.sub
>>> import re
>>> test = "there's a need to capitalize every word"
>>> re.sub(r'\b[a-z]', lambda m: m.group().upper(), test)
"There'S A Need To Capitalize Every Word"
答案 3 :(得分:0)
test = "there's a need to capitalize every word"
print(test.capitalize())