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 Make a Flattened List from Nested List
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Access Index of a List Using for Loop
Python File I/O Operation
Python hash()
Python Program to Print Colored Text to the Terminal
Converting a Stack Trace to a String in Java
Python Program to Get the Full Path of the Current Working Directory
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Randomly Select an Element From the List
Python frozenset()
Python List append()
Python ord()
Python abs()
Python Program to Sort Words in Alphabetic Order
Python vars()
Python Set issuperset()
Python String zfill()
Python if...else Statement
Python Program to Extract Extension From the File Name
Python Program to Convert Decimal to Binary Using Recursion
Python Get Current time
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python oct()
Python String partition()
Python Program to Check If a List is Empty
JavaScript Eval: run a code string
Python List copy()
Java InputStream to String
Python for Loop
Python Program to Check if a Number is Odd or Even
Python Program to Iterate Over Dictionaries Using for Loop