'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. But i have no idea how to capitalize it. Any help is appreciated!



Solution 1:[1]

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.

Solution 2:[2]

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.

If you want every letter uppercase use 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'

Solution 3:[3]

You can use the title method of string class which capitalizes the first letters of every word in a sentence:

my_list = ['apple pie', 'orange jam']
print my_list[0].title()

result:

Apple Pie

or capitalize method which only capitalizes the first letter:

my_list = ['apple pie', 'orange jam']
print my_list[0].capitalize()

result:

Apple pie

Solution 4:[4]

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:

>>> 'test code'.capitalize()
'Test code'

>>> '_test'.capitalize()
'_test'

Solution 5:[5]

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 

Solution 6:[6]

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)

Solution 7:[7]

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'

Here I have Converted the list(x) into string by str(x) so we can use functions like title or capitalize in string(x). You can use dir(x) to see which function can be used for that object x in string. I have used title function, you can use title or capitalize and the replaced it with nothing. By using Join it is combined Everything. Please comment on my steps and working process. Thank You.

Solution 8:[8]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MendelG
Solution 2
Solution 3 Selcuk
Solution 4 dhokas
Solution 5 ForsakenOne
Solution 6 Gorka
Solution 7 Hardik Parmar
Solution 8 Ersel Er