Reference Materials
Created with over a decade of experience.
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python String Methods
- Python String capitalize()
- Python String center()
- Python String casefold()
- Python String count()
- Python String endswith()
- Python String expandtabs()
- Python String encode()
- Python String find()
- Python String format()
- Python String index()
- Python String isalnum()
- Python String isalpha()
- Python String isdecimal()
- Python String isdigit()
- Python String isidentifier()
- Python String islower()
- Python String isnumeric()
- Python String isprintable()
- Python String isspace()
- Python String istitle()
- Python String isupper()
- Python String join()
- Python String ljust()
- Python String rjust()
- Python String lower()
- Python String upper()
- Python String swapcase()
- Python String lstrip()
- Python String rstrip()
- Python String strip()
- Python String partition()
- Python String maketrans()
- Python String rpartition()
- Python String translate()
- Python String replace()
- Python String rfind()
- Python String rindex()
- Python String split()
- Python String rsplit()
- Python String splitlines()
- Python String startswith()
- Python String title()
- Python String zfill()
- Python String format_map()
Python String capitalize()
The capitalize() method converts the first character of a string to an uppercase letter and all other alphabets to lowercase.
Example
sentence = "i love PYTHON"
# converts first character to uppercase and others to lowercase
capitalized_string = sentence.capitalize()
print(capitalized_string)
# Output: I love python
capitalize() Syntax
The syntax of the capitalize() method is:
string.capitalize()
capitalize() Parameter
The capitalize() method doesn't take any parameter.
capitalize() Return Value
The capitalize() method returns:
- a string with the first letter capitalized and all other characters in lowercase.
Example 1: Python capitalize()
sentence = "python is AWesome."
# capitalize the first character
capitalized_string = sentence.capitalize()
print(capitalized_string)
Output
Python is awesome.
In the above example, we have used the capitalize() method to convert the first character of the sentence string to uppercase and the other characters to lowercase.
Here, sentence.capitalize() returns "Python is awesome" which is assigned to capitalized_string.
Example 2: capitalize() Doesn't Change the Original String
The capitalize() method returns a new string and doesn't modify the original string. For example:
sentence = "i am learning PYTHON."
# capitalize the first character
capitalized_string = sentence.capitalize()
# the sentence string is not modified
print('Before capitalize():', sentence)
print('After capitalize():', capitalized_string)
Output
Before capitalize(): i am learning PYTHON. After capitalize(): I am learning python.
Here, the capitalize() method doesn't modify the original sentence string.
Also Read:
