PythonString methods
str. capitalize()
Returns a string with the first letter uppercase and the rest lowercase.
- Returns
- A new string.
- Changes the original
- No
Example
#Python
print("welcome to codeworlds".capitalize())
print("i love Python".capitalize())
text = "i love Python"
print(text[:1].upper() + text[1:])Resultsaved result, you can check it
Welcome to codeworlds
I love python
I love Python
Definition and usage
#The capitalize() method converts the first character of the string to uppercase and all the others to lowercase. That makes a sentence look like a normal English sentence.
Careful: it also lowercases letters that were intentionally uppercase, such as proper nouns in the middle of a sentence. "i love Python".capitalize() gives "I love python".
If you only want to raise the first letter without changing the rest, use slicing: text[:1].upper() + text[1:].
Syntax
#Syntax
string.capitalize()Good practices
#- When the text contains proper nouns, raise only the first letter with a slice instead of
capitalize(). - To capitalize each word individually, use
title().
Related entries
#Spotted a mistake or missing an example? Write to us.