python - 如何将字符串中每个单词的首字母大写?

标签 python capitalization capitalize

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/

相关文章:

python - Phantomjs Selenium WebDriver 中的自定义 header

c++ - 大写字符,topper 是如何工作的?

android - 有没有办法将 TextView 的所有字母设置为大写?

python - 如何将一行输出作为字符串?

java - 将文本文件中的字母大写并保存更改 - Java

python - 如何在 Numpy 中使用 savetxt 有两个前导空格

使用 panic CODA 的 Python 缩进问题

c - C中每个单词首字母的简单大写

css - 将动态元标题中的一个词大写

python - pytest 下未报告 doctest 异常输出