Python String capitalize()

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

The capitalize() is a built-in string method with the following syntax:

str.capitalize()Code language: CSS (css)

The capitalize() method takes no argument and returns a copy of the str with its first character capitalized and the other characters lowercased.

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

Njemačka 

However, the capitalize() method returns the NJemačka:

print('Njemač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 title() method.

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)

Output:

Now is better than never.

Summary

  • 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.
Did you find this tutorial helpful ?