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
1 2 3 | my_string = "programiz is Lit" print (my_string[ 0 ].upper() + my_string[ 1 :]) |
Output
1 | 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()
1 2 3 4 5 | my_string = "programiz is Lit" cap_string = my_string.capitalize() print (cap_string) |
Output
1 | Programiz is lit |
Note: capitalize() changes the first character to uppercase; however, changes all other characters to lowercase.
Related posts:
Python Program to Convert Celsius To Fahrenheit
Python List extend()
Python Program to Get the File Name From the File Path
Python help()
Python String casefold()
Python callable()
Python *args and **kwargs
Split a String in Java
Python Closures
Python Program to Create a Countdown Timer
Python memoryview()
Python abs()
Python Program to Find the Square Root
Python Tuple count()
Python Program to Print Colored Text to the Terminal
Python ascii()
Python Program to Remove Duplicate Element From a List
Python bytes()
Python Set clear()
Python Program to Get the Class Name of an Instance
Python Global, Local and Nonlocal variables
How to get current date and time in Python?
Python Program to Print Hello world!
Python List index()
Python List sort()
Python type()
Python Set pop()
Python input()
Python Program to Find the Factorial of a Number
Python Modules
Python Statement, Indentation and Comments
Python min()