Summary: in this tutorial, you’ll learn how to use the Python string
capitalize() method to return a copy of a string with its first character capitalized.
Introduction to the Python
capitalize() is a built-in string method with the following syntax:
str.capitalize()Code language: CSS (css)
capitalize() method takes no argument and returns a copy of the
str with its first character capitalized and the other characters lowercased.
capitalize() makes the first character of a string uppercase, which works fine for ASCII. However, it won’t work for some non-English letters.
For example, the letter
NJ in Croatian appears as
Nj at the start of words when the first character is uppercased:
capitalize() method returns the
print('ǋemačka'.capitalize())Code language: PHP (php)
To fix this issue, starting from Python 3.8, the
capitalize() method puts the first character of the string into titlecase rather than uppercase.
Note that to capitalize the first letter of every word in a string, you use the string
Python capitalize() method example
The following example shows how to use the
capitalize() method to return a copy of a string with the first character converted to titlecase:
s = 'now is better than never.' new_s = s.capitalize() print(new_s)Code language: PHP (php)
Now is better than never.
- Use the Python string
capitalize()method to return a copy of a string with the first character converted to titlecase and the other characters converted to lowercase.