Python String capitalize() function returns the capitalized version of the string. The first character of the returned string is converted to uppercase and rest of the characters are changed to lowercase.
Python String capitalize()
Let’s look at an example of string capitalize() function.
Copys = 'WelCome to JournalDev' print(s.capitalize())
Welcome to journaldev
Let’s look at another example where string first character is not an ASCII character.
Copys = '歡迎' # welcome in Chinese print(s.capitalize()) s = 'çA†' print(s.capitalize())
The Chinese capitalized version looks same as the original string. But I don’t know the Chinese language, so I am not sure if it’s correct or not.
For the special character, it looks like the capitalization worked and the first character got changed to uppercase.
Finally, let’s look at an example of getting user input and capitalize the input string and print it.
Copys = input('Please enter a string:\n') print('Input String Capitalized Version:\n', s.capitalize())
Copy>>> s = input('Please enter a string:\n') Please enter a string: apple çÇ†ˇƒÏ >>> print('Input String Capitalized Version:\n', s.capitalize()) Input String Capitalized Version: Apple çç†ˇƒï >>>
Reference: Official Documentation