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 Factorial of a Number
Python Dictionary
Intelligent Projects Using Python - Santanu Pattanayak
Python String split()
Python Global, Local and Nonlocal variables
Python sum()
Python pass statement
Python Program to Safely Create a Nested Directory
Python Set symmetric_difference_update()
Python Set discard()
Java InputStream to String
Python @property decorator
Python Global Keyword
Python Program to Print Colored Text to the Terminal
Python timestamp to datetime and vice-versa
Python bin()
Python String isupper()
Python String swapcase()
Python Program to Count the Number of Each Vowel
Python String join()
String Hashing
Reading an HTTP Response Body as a String in Java
Python List sort()
Python setattr()
Python String endswith()
Python Set isdisjoint()
Python List remove()
Python List clear()
Python Program to Count the Occurrence of an Item in a List
Python String isprintable()
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Print Output Without a Newline