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!
-
1possible duplicate of [How to capitalize the first letter of each word in a string (Python)?](http://stackoverflow.com/questions/1549641/how-to-capitalize-the-first-letter-of-each-word-in-a-string-python) – dting Mar 29 '15 at 20:14
-
If you have received answers and are satisfied with the solution, then please consider marking it as Accepted. This helps others having the same issue get the solution quicker and stop others from answering unnecessarily – Fardeen Khan Mar 10 '22 at 05:35
8 Answers
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.

- 14,885
- 4
- 25
- 52

- 402
- 5
- 12
-
-
2As I said, just replace the .capatalize() with .title() - that will capatilize every word, thus it will capatilize the last names. – Beatrix Kidco Oct 31 '17 at 12:19
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'

- 176,452
- 29
- 245
- 321
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

- 57,004
- 12
- 102
- 110
-
It only prints the first word, I am trying to get the whole sentence printed out with only the first letter being capitalized and the rest are normal. – Mark Kent Mar 29 '15 at 20:17
-
-
My list is basically anything that user types in. I am splitting up a sentence into a list, then I am manipulating some words and converting them back into a string. But I am just trying to make the first letter capitalized, the rest doesn't really matter. So, it could look like "I am going to the store", and the list is just gonna be all of these words. – Mark Kent Mar 29 '15 at 20:19
-
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'

- 1,771
- 2
- 13
- 22
-
You may want to mention that it will also make every other character lowercase. `'hello Thomas'.capitalize()` returns `'Hello thomas'` – TomNorway Dec 28 '16 at 16:12
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

- 11
- 2
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)

- 1,971
- 1
- 13
- 28

- 11
- 1
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.

- 11
- 2
-
Welcome to stackoverflow, Hardik, and thanks for your contribution. Unfortunately, I don't think this will work very well. Some of the strings may contain '[' or ']' characters. Also the join is a no-op since x is already simply a string. Finally, you end up with a single string and have lost the list structure... that's probably not the intention of the person asking the question. The hint about using ``dir(x)`` is quite useful. You could also have pointed to the python string documentation. – Matthias C. M. Troffaes Jun 27 '21 at 08:25
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)

- 46
- 1
- 7
-
This will capitalize all the words in the string and not just the first one I believe – Fardeen Khan Mar 10 '22 at 05:38
-
Just the first one. Give a list with string and try: l = [i.title() for i in a] – Koosul Mar 10 '22 at 08:35