In this tutorial, we’ll learn how to properly use Python string capitalize() method with the help of easy Python code examples.
Introduction: Python String capitalize() Method
This method is used to convert the first letter of a string to uppercase and all the other letters to lower case. Let’s first understand its syntax then we’ll implement it using practical code examples.
Syntax of capitalize() Method
- Python string capitalize method does not take any arguments.
- It returns a string passed to it as an argument with a format of capital(uppercase) first letter and all the other letters converted to small(lowercase).
Example 1: Implementing capitalize() Method on a String
stringValue= 'i Am A PakIsTANi' print(stringValue.capitalize())
I am a pakistani
As you can see here that the first letter of string is converted to uppercase(capital) and all the others to lowercase.
You can use a direct string as well. See below code:
print( 'mY namE IS ZeesHAn'.capitalize() )
My name is zeeshan
Example 2: capitalize() Method Return Value
originalString= 'i Am YasIR' newString=originalString.capitalize() print(newString) print(originalString)
I am yasir i Am YasIR
We can see that a new string is returned by Python string capitalize() method. It does not modify the existing string which can be seen in the above output.
In conclusion of this tutorial, now you have a practical knowledge of how to easily use Python string capitalize method. I’ll be looking forward at your feedback. Thank you for reading this post.