Python Capitalize First Letter

Python capitalize first letter | In this post, we will develop a Python program to capitalize the first letter of a sentence. If the string is “know program” then after capitalization string will be “Know program”. We will also develop a Python program to capitalize the first letter of every word in a string. If the string is “know program” then after capitalization string will be “Know Program”.

Python Capitalize First Letter of Sentence

We will take a string while declaring the variable. Then, Python capitalize first letter of the string using capitalize() function and Regular Expression. Finally, printing the new capitalize string.

Using capitalize() function

The capitalize() function returns a string where converts the first character of the string to a capital (uppercase) character while the remaining characters are lowercase letters.

# Python program to capitalize the first letter of string

# take string
string = input('Enter any string: ')

# capitalize using capitalize() function
cap_string = string.capitalize()

# printing capitalize string
print('Capitalized String:', cap_string)

Output for the different input values:-

Enter any string: python
Capitalized String: Python

Enter any string: know program
Capitalized String: Know program

Using Regular Expression

We will also capitalize the first letter of string using Regular Expression (RegEx module). This method will capitalize the first letter of every word in the string while all remaining characters are lowercase letters.

# Python program to capitalize the first letter of string

# importing RegEx module
import re

# take string
string = input('Enter any string: ')

# capitalize using capwords() function
cap_string = re.sub('([a-zA-Z])', lambda x: x.groups()[0].upper(), string, 1)

# printing capitalize string
print('Capitalized String:', cap_string)

Output:-

Enter any string: first letter
Capitalized String: First letter

The sub() function replaces the string pattern [a-zA-Z] and lambda x: x.groups()[0].upper() converts the first matched group in the regular expression. re.sub() function is set to be 1 to make the replacement only once.

Python Capitalize First Letter of Every Word in String

In the previous program, we will capitalize the first letter in a string but in this program, capitalize the first letter of every word in a string using title() and capwords() function.

Using title() function

The title() function returns a string where converts the first character of each word to an uppercase letter while all remaining characters of the word are lowercase letters.

# Python program to capitalize first letter of every word

# take string
string = input('Enter any string: ')

# capitalize using title() function
cap_string = string.title()

# printing capitalize string
print('Capitalized String:', cap_string)

Output:-

Enter any string: first letter
Capitalized String: First Letter

Using capwords() function

We will also capitalize the first letter of every word in string using capwords() function. It is the function of the string module. Python’s string module provides a function capwords() to the first character of each word to the uppercase letter while all remaining characters of a word are lowercase letters.

# Python program to capitalize first letter of every word

# importing string module
import string

# take string
new_string = input('Enter any string: ')

# capitalize using capwords() function
cap_string = string.capwords(new_string)

# printing capitalize string
print('Capitalized String:', cap_string)

Output:-

Enter any string: capwords function
Capitalized String: Capwords Function

Also See:- Python Program to Convert Lowercase to Uppercase

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *