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 setattr()
Encode a String to UTF-8 in Java
Python Program to Print Output Without a Newline
Python Set intersection_update()
Array to String Conversions
Python max()
Python Get Current time
Python String expandtabs()
Python List index()
Python Program to Add Two Numbers
Python String encode()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Dictionary get()
Python Program to Print all Prime Numbers in an Interval
JavaScript Eval: run a code string
Python Input, Output and Import
Python len()
Python locals()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python del Statement
Python List Comprehension
Python Program to Check If a String Is a Number (Float)
Python issubclass()
Convert String to int or Integer in Java
Python Program to Sort a Dictionary by Value
Generate a String
Python Program to Find HCF or GCD
Python Program to Find Armstrong Number in an Interval
Python reversed()
Python Set issuperset()
Python List extend()
Most commonly used String methods in Java