ItsMyCode |
Python string capitalize() method will convert the first letter in a string to uppercase and keep the rest of the characters in lowercase. The capitalize()
function does not modify the original string and instead returns a string copy.
capitalize() Syntax
The syntax of capitalize()
method is:
string.capitalize()
capitalize() Parameter
The capitalize()
function does not take any parameters.
Return Value from capitalize()
The capitalize()
function returns a copy of the string with the first character capitalized and all other characters lowercased. It doesn’t modify the original string.
Note: In Python 3.8 onwards, the first character is converted into a title case rather than uppercase. It means that characters like digraphs will only have their first letter capitalized instead of the whole character.
Example: Capitalize a string in Python
# Converts the first character to Uppercase/title case
# keeps rest in lowercase
text1 = "python programming"
print(text1.capitalize())
# Converts the first character to Uppercase/title case
# keeps rest in lowercase
text2= "pYTHON Is FUN"
print(text2.capitalize())
# In case of diagraph the first letter is capitalized
text3= "ß"
print(text3.capitalize())
# In case of non alphabets
text4= "*disclaimer"
print(text4.capitalize())
Output
Python programming
Python is fun
Ss
*disclaimer
The post Python String capitalize() appeared first on ItsMyCode.
from Planet Python
via read more
No comments:
Post a Comment