Many times when you are working in Python or some other program langauge. So you have to do modification/manipulation etc. of strings. Today we will learn to capitalize strings first character or convert string first character/letter to uppercase in Python.
In this post, we will discuss how to convert the first character/letter of string to uppercase.
Python String capitalize()
Python capitalize method is one of the Python String Method, which is used to convert the first character into Uppercase and remaining characters to Lowercase and return a new string.
The syntax of capitalize() is:
The capitalize() function doesn’t take any parameter.
Return Value from capitalize()
The capitalize() function returns a string with first letter capitalized and all other characters lowercased. It doesn’t modify the original string.
Now you know that How to convert first character/letter of string to uppercase in Python. So now we discuss how to write a capitalize Function in Python programs with an example:
Example 1: Capitalize a Sentence
string = "python is AWesome." capitalized_string = string.capitalize() print('Old String: ', string) print('Capitalized String:', capitalized_string)
Example 2: Non-alphabetic First Character
string = "+ is an operator." new_string = string.capitalize() print('Old String:', string) print('New String:', new_string)