如何将字符串中每个单词的首字母大写? [英] How to capitalize the first letter of each word in a string?

查看:219
本文介绍了如何将字符串中每个单词的首字母大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

s = 'the brown fox'

。在这里做某事...

...do something here...

s 应该是:

'The Brown Fox'

最简单的是什么

推荐答案

.title() 方法(可以使用ASCII或Unicode都可以这样做):

The .title() method of a string (either ASCII or Unicode is fine) does this:

>>> "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"


这篇关于如何将字符串中每个单词的首字母大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆