尝试大写多行字符串,但不起作用。有什么想法吗?

时间:2018-12-20 03:17:52

标签: python python-3.x capitalization capitalize

text = '''felt happy because I saw the others were happy 
 and because I knew I should feel happy, 
 but I wasn’t really happy.'''

print(text.capitalize())

只有第一个单词大写。

2 个答案:

答案 0 :(得分:3)

official documentation关于capitalize()

  

返回字符串的副本,其首字符大写并   其余的都小写。

您要寻找的方法是title(),该方法将大写每个单词。这是the documentation的描述:

  

返回字符串的标题大写版本,其中单词以大写字母开头,其余字符为小写字母。

这是结果:

>>> text = '''felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy.'''
>>> print(text.title())

'Felt Happy Because I Saw The Others Were Happy And Because I Knew I Should Feel Happy, But I Wasn’T Really Happy.'

答案 1 :(得分:2)

$('#title-Checklist-'+configurationId+'-name') is documented仅将整个字符串的第一个字符大写,其余的则小写:

  

str.capitalize()

     
    

返回字符串的副本,该字符串的首个字符大写,其余小写。

  

如果您想将每个单词的首字母大写(其余部分用小写),请use .title();如果要每个字符都大写,请use .upper()