Python String capitalize()

Filed Under: Python

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

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 special characters

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 capetalize user input


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

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages