Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter of the first word in a list in Python

Tags:

python

list

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!

like image 569
Mark Kent Avatar asked Mar 29 '15 20:03

Mark Kent


People also ask

How do you capitalize the first letter of a word in a list in Python?

string capitalize() in Python Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter, while making all other characters in the string lowercase letters.

Do you capitalize the first word in a list?

Lowercase is preferred for list items that, together with an introductory phrase, form a complete sentence (Example A). Initial capitalization is preferred for list items that are complete sentences (Example B) or stand-alone phrases (Examples C and D).

How do you capitalize words in Python?

To capitalize the first letter, use the capitalize() function. This functions converts the first character to uppercase and converts the remaining characters to lowercase. It doesn't take any parameters and returns the copy of the modified string.

How do you capitalize the first letter of each word in a string?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.


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

like image 91
Beatrix Kidco Avatar answered Oct 12 '22 04:10

Beatrix Kidco