Python String capitalize() Method
By Lenin Mishra
If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.
The capitalize()
method in Python returns a copy of the string with only the first character capitalized.
Code
str1 = "pylenin writes about python."
print(str1.capitalize())
Output
Pylenin writes about python.
If you want to capitalize the first letter of every word, perform something like this.
Code
str1 = "pylenin writes about python."
str_list = str1.split(" ")
new_string = ' '.join(x.capitalize() for x in str_list)
print(new_string)
Output
Pylenin Writes About Python.
Check out other commonly used Python string methods.
Related Articles
- How to create a string in Python?
- How to access characters in a Python string?
- How to replace characters in a string in Python?
- How to concatenate strings in Python?
- How to iterate through a string in Python?
- Check if a Substring is Present in a Given String in Python
- Escape sequences in Python String
- Python String Formatting - The Definitive Guide