简体   繁体   English

Python中列表中第一个单词的首字母大写

[英]Capitalize first letter of the first word in a list in Python

I am using list[0][0] to find the first letter of the first word in a list.我正在使用list[0][0]查找列表中第一个单词的第一个字母。 But i have no idea how to capitalize it.但我不知道如何大写它。 Any help is appreciated!任何帮助表示赞赏!

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

Output:输出:

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()将每个单词大写。

You can use str.capitalize() to capitalise each string.您可以使用str.capitalize()将每个字符串大写。 If you have any other uppercase letters in the string they will be lowered which may or may not be relevant.如果字符串中有任何其他大写字母,它们将被降低,这可能相关也可能不相关。

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'

You can use the title method of string class which capitalizes the first letters of every word in a sentence:您可以使用string类的title方法,它将句子中每个单词的首字母大写:

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

There are 2 functions to do this, title and capitalize.有 2 个函数可以做到这一点,标题和大写。

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'

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 

You can use camelcase package:您可以使用驼峰包:

import camelcase

list_name = input("Enter a list of names")
print(list_name)
cm = camelcase.CamelCase()
list_name = cm.hump(list_name)
x = ['roger federer', 'timothy olyphant', 'rani laxmibai', 'lata mangeshkar']
x = str(x)
x =(x.title().replace('[','').replace(']',''))
x = ''.join(x)
print(x)

Solution - 'Roger Federer', 'Timothy Olyphant', 'Rani Laxmibai', 'Lata Mangeshkar'解决方案 - 'Roger Federer', 'Timothy Olyphant', 'Rani Laxmibai', 'Lata Mangeshkar'

Here I have Converted the list(x) into string by str(x) so we can use functions like title or capitalize in string(x).在这里,我通过str(x)将 list(x) 转换为字符串,因此我们可以在 string(x) 中使用 title 或大写等函数。 You can use dir(x) to see which function can be used for that object x in string.您可以使用dir(x)查看哪个函数可用于字符串中的对象 x。 I have used title function, you can use title or capitalize and the replaced it with nothing.我用过title函数,你可以用title或者大写,然后什么都没有。 By using Join it is combined Everything.通过使用 Join 它结合了一切。 Please comment on my steps and working process.请评论我的步骤和工作流程。 Thank You.谢谢你。

a = ['alpha', 'bravo','rocky']


def listC(a):


    l = [i.title() for i in a] # [expression for item in list]
    return l

l = listC(a)

print(l)
fruits=["apple", "orange", "grapes","jackfruit"]
for fruit in fruits:
    print(fruit.capitalize())
from nltk.book import text6    
title_words = [word for word in text6 if word.istitle()]

print(len(title_words))

Output:输出:

2672

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM