Python capitalize first letter of sentence
In the tutorial, you will learn how to capitalize the first letter of a sentence using the Python programming language.
In Python, strings are immutable, indexable, and iterable. Strings are one of the most commonly used data structures. In the development process, you may come across a situation where you need to capitalize all the first letter of sentence.
Python has an inbuilt method called capitalize() that converts the first character of a string into an uppercase letter and changes all other characters into lowercase. This can be useful if you have a lot of text that you want to format as uppercase automatically.
Syntax of capitalize()string.capitalize()
This method does not take any parameter. It returns a string where the first character is upper case without modifying the original string. The original string remains unchanged.
Python capitalize first letter
Here is a simple example of capitalising the first letter of a sentence in the Python programming language.
txt1 = "python is a HiGH level general purpose OPEN source programming language."
capitalized_string1 = txt1.capitalize()
print('Original String: ', txt1)
print('Capitalized String:', capitalized_string1)
txt2 = "welcome to Etutorialspoint"
capitalized_string2 = txt2.capitalize()
print('Original String: ', txt2)
print('Capitalized String:', capitalized_string2)
Output of the above code-
Original String: python is a HiGH level general purpose OPEN source programming language.
Capitalized String: Python is a high level general purpose open source programming language.
Original String: welcome to Etutorialspoint
Capitalized String: Welcome to etutorialspoint
Python capitalize first letter using string upper() function
The string upper() method converts all lowercase characters in a string into uppercase characters. We can use this function with string slicing to capitalize the first letter of the sentence. The following example demonstrates this-
string = "python is a HiGH level general purpose OPEN source programming language."
print("Original string:")
print(string)
result = string[0].upper() + string[1:]
print("Capitalized String:",result)
Output of the above code-
Original string:
python is a HiGH level general purpose OPEN source programming language.
Capitalized String: Python is a HiGH level general purpose OPEN source programming language.
Related Articles
Python Convert String to Uppercase
Convert string to lowercase Python
Count vowels in a string Python
Python split strings by comma
Count consonants in a string Python
Reverse pyramid pattern in Python
Fizzbuzz program in Python
Python split string by comma
Python alive progress bar
Python progress bar tqdm
Python language translator code
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword