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 ASCII Value of Character
Python strftime()
Python Program to Find the Factors of a Number
Python String endswith()
Python __import__()
Java Program to Permute All Letters of an Input String
Python Dictionary popitem()
Python Program to Trim Whitespace From a String
Python Program to Convert Decimal to Binary Using Recursion
Python range()
Python min()
Python Operator Overloading
Python Set isdisjoint()
Python Functions
Python Decorators
Converting a Stack Trace to a String in Java
Python staticmethod()
Python Program to Iterate Through Two Lists in Parallel
Convert String to int or Integer in Java
Python Program to Get the Last Element of the List
Python classmethod()
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Find All File with .txt Extension Present Inside a Directory
Python String encode()
Node.js vs Python for Backend Development
Python Program to Check If a List is Empty
Python String isupper()
Converting a Stack Trace to a String in Java
Python Program to Count the Occurrence of an Item in a List
Generate a String
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Check if a Key is Already Present in a Dictionary