Python String capitalize()
Let’s look at an example of string capitalize() function.
s="WelCome to JournalDev"
print(s.capitalize())
Output: Welcome to journaldev
Let’s look at another example where string first character is not an ASCII character.
s="歡迎" # welcome in Chinese
print(s.capitalize())
s="çA†"
print(s.capitalize())
Output:
歡迎
Ça†
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.
s = input('Please enter a string:n')
print('Input String Capitalized Version:n', s.capitalize())
>>> s = input('Please enter a string:n')
Please enter a string:
apple çdžˇƒÏ
>>> print('Input String Capitalized Version:n', s.capitalize())
Input String Capitalized Version:
Apple ç熡ƒï
>>>
Reference: Official Documentation