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!
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With