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 Factors of a Number
Python String swapcase()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Iterate Through Two Lists in Parallel
Python hash()
Python range()
Python String expandtabs()
Python Set issuperset()
Python Dictionary setdefault()
Python Set issubset()
Python Program to Copy a File
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Sort Words in Alphabetic Order
Python time Module
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String splitlines()
Python Program Read a File Line by Line Into a List
Python Set union()
Python Object Oriented Programming
Python Dictionary copy()
Python List clear()
Python String title()
Python Program to Find the Factorial of a Number
Python isinstance()
Python Program to Check Prime Number
Python Data Types
CharSequence vs. String in Java
Python String isupper()
Python Set isdisjoint()
Python String ljust()
Python @property decorator
Python Data Structures and Algorithms - Benjamin Baka