Python

Capitalize the first letter of a string with python capitalize() function

Capitalize the first letter of a string with python capitalize() function
Python has a built-in method named capitalize() to convert the first character of a string into uppercase and change the rest of the characters into lowercase. This method can be used on string data in various ways without just capitalizing on the first characters. How you can apply this method in python script in different ways are shown in this article.

Syntax:

string.capitalize()

This method does not use any argument and it returns a new string after modifying the content of the original string. The original string remains unchanged. The uses of this method on various types of data in python are explained below with examples.

Example-1: Use capitalize method on a simple string

The capitalize() method is applied in three different types of text data in the example.  At first, a text starts with the lowercase letter is used for conversion. The first character of the text will be uppercase and the rest of the characters of the text will be lowercase by capitalize() method. Next, a text with all uppercase letters is used for conversion and a text starts with number is used for conversion.

#!/usr/bin/env python3
# Define a string value
myString = 'welcome to LinuxHint'
# Convert the string by capitalize method
convertedString = myString.capitalize()
# Print the original string
print('The first original string is : %s' %myString)
# Print the converted string
print('The first converted string is : %s\n' %convertedString)
# Define a string with all capital letter
myString2 = 'I LIKE PYTHON PROGRAMMING'
# Convert the string by capitalize method
convertedString2 = myString2.capitalize()
# Print the original string
print('The second original string is : %s' %myString2)
# Print the converted string
print('The second converted string is : %s\n' %convertedString2)
# Define a string starting with number
myString3 = '7827 Ridgeview Court Summerville, SC 29483'
# Convert the string by capitalize method
convertedString3 = myString3.capitalize()
# Print the original string
print('The third original string is : %s' %myString3)
# Print the converted string
print('The third converted string is : %s\n' %convertedString3)

Output:

The following output will appear after running the script.

Example-2: Use capitalize method to change each word of a string into uppercase

How the first character of each word in a text can be capitalized is shown in the following example. At first, text value of multiple words will be taken as input from the user. Any text value can be divided into substring using split() method. split() method is used here to divide the text based on space and return a list of words. newString variable is used here to store the converted text. for loop is used to read each item of the list and capitalize the first letter of each item and store the converted value with space in newString.  The previous value of newString will be combined with the new value to generate the desired output.  Next, both original text and converted text are printed to see the difference.

#!/usr/bin/env python3
# Take a string input
text = input("Enter a text\n")
# Split the text based on space
strList = text.split()
# Define a variable to store the converted string
newString = "
# Iterate the list
for val in strList:
# Capitalize each list item and merge
newString += val.capitalize()+ "
# Print the original string
print('The original string is : %s' %text)
# Print the converted string
print('The converted string is : %s\n' %newString)

Output:

In the following output, 'i like python programming' is taken as input and after applying the capitalize() and split() methods, the output is 'I Like Python Programming'.

Example-3: Capitalize the first letter of each sentence in a text of multiple sentences.

In the previous two examples, capitalize() method is applied in a single line of text. But sometimes, it is required to work with a file content or a long text of multiple sentences and needs to capitalize the first letter of each line of the file or capitalize the first letter of each sentence of the text. capitalize() method with split() can be used to solve this problem. The example shows the way to capitalize the first letter of each sentence of a long text. Here, a variable named text is defined with a string value of three sentences. At first, the value of the text is divided based on '.' using split() method to create a list of three sentences. Next, for loop is used to capitalize the first letter of each sentence like example 2. Here, '.' is combined with each converted item to define the end of the line. strip() method is used to remove the unnecessary space and last extra '.' is removed from newText by using position value.

#!/usr/bin/env python3
# Define a long text
text = 'python is an interpreted, high-level, general-purpose programming language.
created by Guido van Rossum. it is first released in 1991.'
# Split the text based on space
lineList = text.split('.')
# Define a variable to store the converted string
newText = "
# Iterate the list
for val in lineList:
# Remove space from starting and ending
val = val.strip()
# Capitalize each list item and merge with '.'
newText += val.capitalize()+'. '
# Remove the last dot
newText = newText[:-2]
# Print the original string
print('The original text is : \n%s' %text)
# Print the converted string
print('\nThe converted text is : \n%s' %newText)

Output:

Both the original text and the converted text are shown in the output.

Conclusion:

When you work with the string data and need to uppercase the first letter of the string or the first letter of each word of the string or the first letter of each sentence of a long text then capitalize() method can be used with another method to do the task. The tasks mentioned here are shown in this article with examples. I hope, this article will help the readers to use capitalize() method on string data efficiently for multiple purposes.

Watch Author's Video: here

WinMouse lets you customize & improve mouse pointer movement on Windows PC
If you want to improve the default functions of your mouse pointer use freeware WinMouse. It adds more features to help you get the most out of your h...
Mouse left-click button not working on Windows 10
If you are using a dedicated mouse with your laptop, or desktop computer but the mouse left-click button is not working on Windows 10/8/7 for some rea...
Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...