Python 中列表中第一个单词的首字母大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29334276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capitalize first letter of the first word in a list in Python
提问by Mark Kent
I am using list[0][0]
to find the first letter of the first word in a list. But i have no idea how to capitalize it. Any help is appreciated!
我正在使用list[0][0]
查找列表中第一个单词的第一个字母。但我不知道如何将其大写。任何帮助表示赞赏!
回答by Selcuk
You can use the title
method of string
class which capitalizes the first letters of every word in a sentence:
您可以使用class的title
方法string
将句子中每个单词的第一个字母大写:
my_list = ['apple pie', 'orange jam']
print my_list[0].title()
result:
结果:
Apple Pie
or capitalize
method which only capitalizes the first letter:
或capitalize
只大写第一个字母的方法:
my_list = ['apple pie', 'orange jam']
print my_list[0].capitalize()
result:
结果:
Apple pie
回答by Padraic Cunningham
You can use str.capitalize()
to capitalise each string. If you have any other uppercase letters in the string they will be lowered which may or may not be relevant.
您可以使用str.capitalize()
大写每个字符串。如果字符串中有任何其他大写字母,它们将被降低,这可能相关也可能不相关。
If you want every letter uppercase use str.upper()
如果你想要每个字母大写使用 str.upper()
In [26]: "foo bar".capitalize() # first letter
Out[26]: 'Foo bar'
In [30]: "foo Bar".capitalize()
Out[30]: 'Foo bar'
In [27]: "foo".upper() # all letters
Out[27]: 'FOO'
回答by dhokas
There are 2 functions to do this, title and capitalize.
有两个函数可以做到这一点,标题和大写。
Title capitalizes the first letter of every word
标题将每个单词的第一个字母大写
>>> 'test code'.title()
'Test Code'
It also "works" if the first character is a digit:
如果第一个字符是数字,它也“有效”:
>>> '_test'.title()
'_Test'
Capitalize will do it for the first word, and do nothing if the first character is not a letter:
Capitalize 将对第一个单词执行此操作,如果第一个字符不是字母,则不执行任何操作:
>>> 'test code'.capitalize()
'Test code'
>>> '_test'.capitalize()
'_test'
回答by Beatrix Kidco
It's actually much simpler than what you think. Follow this code and capitalize ALL or SOME the words that's in your list.
它实际上比你想象的要简单得多。按照此代码并将列表中的所有或部分单词大写。
singers = ['johnny rotten', 'eddie vedder', 'kurt kobain', 'chris cornell', 'micheal phillip jagger']
singers = [singer.capitalize() for singer in singers]
print(singers)
#instead of capitalize use title() to have each word start with capital letter
OUT: Johnny rotten, Eddie vedder, Kurt kobain, Chris cornell, Micheal phillips Jagger
出局:约翰尼·罗滕、埃迪·维德、科特·科班、克里斯·康奈尔、迈克尔·菲利普斯·贾格尔
The names will now be saved in your list in this manner for future use. Use .title() instead of .capitalize() to capitalize every word.
这些名称现在将以这种方式保存在您的列表中以备将来使用。使用 .title() 而不是 .capitalize() 来大写每个单词。
回答by ForsakenOne
For capitalizing all letters in a word list
用于将单词列表中的所有字母大写
fruitlist = ['apple', 'banana', 'cherry', 'durian', 'orange']
for i in fruitlist:
print (i.upper(), end=', ')
If you want just the First letter of every word ...
如果你只想要每个单词的第一个字母......
fruitlist = ['apple', 'banana', 'cherry', 'durian', 'orange']
for i in fruitlist:
print (i.title(), end=', ') #in this case i.capitalize() can also be used
回答by alankrit
from nltk.book import text6
title_words = [word for word in text6 if word.istitle()]
from nltk.book import text6
title_words = [word for word in text6 if word.istitle()]
print(len(title_words))
打印(len(title_words))
Output: 2672
输出:2672