python String capitalize() Method

27-12-22 Ahmed Obaid 1436 0

​The capitalize() function returns a new copy of the original string and converts the first character of the string to a capital letter, while all other characters in the string are lowercase.


How to formulate it like this:


string_name. capitalize()

Argument: The capitalize() function takes no argument.


The capitalize() function returns a new string in which the first character is a capital letter, and the rest of the string is a lowercase letter.

Example:


txt = "learn python with ahmad obaid"
cap = txt.capitalize()
print(cap)

The output will be:


Learn python with ahmad obaid

The capitalize() function returns a new copy of the string and does not modify the original string

Example:


string = "i love python"
print(string)
print(string.capitalize())

The output will be:


i love python
I love python

External sources:

Built-in functions - official Python documentation

If you have any questions or concerns, leave them in the comments



Tags


Python Python String string methods python string capitalize python

Share page