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 String isupper()
Python String splitlines()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python break and continue
Python Program to Check the File Size
Python min()
Python bytes()
How to get current date and time in Python?
Python String maketrans()
Python Set add()
Python Object Oriented Programming
Check if a String is a Palindrome in Java
Python enumerate()
Python zip()
Python format()
Python Program to Delete an Element From a Dictionary
Deep Learning in Python - LazyProgrammer
Python Program to Measure the Elapsed Time in Python
Python super()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Print Output Without a Newline
Python String title()
Python Keywords and Identifiers
Python Dictionary fromkeys()
Python Program to Check Whether a String is Palindrome or Not
Python List Comprehension
Python Program to Illustrate Different Set Operations
Python Program to Display Fibonacci Sequence Using Recursion
Python time Module
Python Set discard()
Convert String to Byte Array and Reverse in Java