Python String capitalize() With Examples

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.


s="WelCome to JournalDev"
print(s.capitalize())

Output: Welcome to journaldev

Python String capitalize() With Examples

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†
Python String capitalize() With Examples

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())

Python String capitalize() With Examples


>>> 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 ç熡ƒï
>>>
You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: