Table of Contents
In Python, the capitalize() method converts first character of a string to uppercase letter and lowercases all other characters, if any.
The syntax of capitalize() is:
string.capitalize()
1. capitalize() Parameter
The capitalize()function doesn’t take any parameter.
2. Return Value from capitalize()
The capitalize() function returns a string with the first letter capitalized and all other characters lowercased. It doesn’t modify the original string.
3. Example 1: Capitalize a Sentence
string = "python is AWesome."
capitalized_string = string.capitalize()
print('Old String: ', string)
print('Capitalized String:', capitalized_string)
Output
Old String: python is AWesome Capitalized String: Python is awesome
4. Example 2: Non-alphabetic First Character
string = "+ is an operator."
new_string = string.capitalize()
print('Old String:', string)
print('New String:', new_string)
Output
Old String: + is an operator. New String: + is an operator.
Related posts:
String Processing with Apache Commons Lang 3
Python Program to Get File Creation and Modification Date
Python Program to Display Fibonacci Sequence Using Recursion
Converting String to Stream of chars
Python Object Oriented Programming
Python String endswith()
Node.js vs Python for Backend Development
Python tuple()
Python Dictionary fromkeys()
Python chr()
Python RegEx
Python hasattr()
Python List extend()
Python Program to Transpose a Matrix
Python Program to Get a Substring of a String
Python dict()
Python List Comprehension
Python Program to Differentiate Between type() and isinstance()
Python frozenset()
Python sum()
Python Program to Compute all the Permutation of the String
String Set Queries
Python String isspace()
Python Strings
Python List reverse()
Python Program to Count the Occurrence of an Item in a List
Python strptime()
Python String isnumeric()
Python String islower()
Python Dictionary setdefault()
Python Program to Remove Duplicate Element From a List
Python Program to Check the File Size