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 Find the Largest Among Three Numbers
Python Dictionary
Python Data Structures and Algorithms - Benjamin Baka
Python Set discard()
Python Program to Get the File Name From the File Path
Python Program to Catch Multiple Exceptions in One Line
Python String split()
Python Program to Display the multiplication Table
Python Set symmetric_difference()
Python vars()
Python Program to Make a Flattened List from Nested List
Python Program to Create Pyramid Patterns
Python isinstance()
Python hasattr()
Python bytes()
Python String format_map()
Python Machine Learning - Sebastian Raschka
Python Program to Iterate Through Two Lists in Parallel
Python Program to Swap Two Variables
Python Dictionary values()
Python del Statement
Python Program to Find Hash of File
Python Program to Find the Size (Resolution) of a Image
Python repr()
Format ZonedDateTime to String
Python Program to Check the File Size
Python int()
Python memoryview()
Python Set add()
Python Program to Access Index of a List Using for Loop
Python @property decorator
Python globals()