String capitalize()
Python String capitalize() method converts the first character in the string to upper case.
In this tutorial, we will learn the syntax and examples for capitalize() method of String class.
ADVERTISEMENT
Syntax
The syntax of String capitalize() method in Python is
str.capitalize()
Examples
Capitalize a String
In this example, we will take a string, and capitalize the first character of this string using str.capitalize() method.
Example.py
x = 'hello world!' result = x.capitalize() print(result)Try Online
Output
Hello world!
Capitalize a String that is Already Capitalized
If the given string is already capitalized, then str.capitalize() does nothing.
Example.py
x = 'Hello world!' result = x.capitalize() print(result)Try Online
Output
Hello world!
Conclusion
In this Python Tutorial, we learned how to capitalize the first character in string using String method – capitalize().