s = 'the brown fox'
...在这里做点什么...
s
应该是:
'The Brown Fox'
最简单的方法是什么?
最佳答案
.title()
字符串的方法(ASCII 或 Unicode 都可以)这样做:
>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'
但是,请注意带有嵌入撇号的字符串,如文档中所述。
The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:
>>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk"
关于python - 如何将字符串中每个单词的首字母大写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1549641/