In this example, you will learn to capitalize the first character of a string.
To understand this example, you should have the knowledge of the following Python programming topics:
- Python Strings
- Python String upper()
- Python String capitalize()
Two strings are said to be anagram if we can form one string by arranging the characters of another string. For example, Race and Care. Here, we can form Race by arranging the characters of Care.
1. Example 1: Using list slicing
my_string = "programiz is Lit" print(my_string[0].upper() + my_string[1:])
Output
Programiz is Lit
In the above example, my_string[0] selects the first character and upper() converts it to uppercase. Likewise, my_string[1:] selects the remaining characters as they are. Finally they are concatenated using +.
2. Example 2: Using inbuilt method capitalize()
my_string = "programiz is Lit" cap_string = my_string.capitalize() print(cap_string)
Output
Programiz is lit
Note: capitalize() changes the first character to uppercase; however, changes all other characters to lowercase.
Related posts:
Python Program to Get a Substring of a String
Python Program to Represent enum
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python chr()
Python Program to Calculate the Area of a Triangle
Python String find()
Python Program to Print all Prime Numbers in an Interval
Python set()
Count Occurrences of a Char in a String
Python break and continue
Python String swapcase()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python super()
Python Program to Remove Punctuations From a String
Python Program Read a File Line by Line Into a List
Python Program to Delete an Element From a Dictionary
Python Deep Learning Cookbook - Indra den Bakker
Python eval()
Python Directory and Files Management
Python Set clear()
Python print()
Python Program to Generate a Random Number
Check If a String Is Numeric in Java
Python Multiple Inheritance
Python max()
Python Matrices and NumPy Arrays
Python issubclass()
Python List clear()
Python Set intersection_update()
Python String isalnum()
Python Dictionary get()