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 Slice Lists
Python Program to Find Factorial of Number Using Recursion
Node.js vs Python for Backend Development
Python Modules
Python bytearray()
Python Objects and Classes
Python Program to Convert Bytes to a String
Python map()
Python input()
Python String isprintable()
Java Program to Implement the Program Used in grep/egrep/fgrep
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Convert String to Byte Array and Reverse in Java
Python abs()
How to get current date and time in Python?
Python Set add()
Python break and continue
Python String isspace()
Python Errors and Built-in Exceptions
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Convert Kilometers to Miles
Python String zfill()
Python Set discard()
Python Program to Split a List Into Evenly Sized Chunks
Python min()
Python strptime()
Python Program to Find LCM
Python List insert()
Python Generators
Python Program to Get the Full Path of the Current Working Directory
Python String casefold()
Python type()