Python has a built-in method called uppercase() to convert the first character in the string to uppercase and replace the other characters with lowercase letters. This method can be applied to string data in different ways, not just by recording the first few characters. This article shows how this method can be used in different ways in a Python script.
Syntax:
This method does not use arguments and returns a new line after the content of the original line has been changed. The original line remains unchanged. The application of this method to different types of data in Python is explained in the following examples.
Example 1: Use the one-line capitalization method
In the example, the capize() method is used in three different types of text data. First of all, for the conversion text is used that starts with a lowercase letter. The first character of the text is in capitals, while the other characters of the text are in lowercase thanks to the capital() method. Then the text with all capital letters is used for the conversion, and the text beginning with a number is used for the conversion.
/usr/bin/env python3
# Set the value of the string
myString = ‘Welcome to LinuxHint’.
# String conversion with method
convertedString = myString.capitalize()
# Print original line
( first original line: %s %myString )
# Print the converted string
(First converted string: %sn %convertString)
# Set an uppercase character
myString2 = I HOW TO PROGRAM PYTHON.
# String conversion with method
convertedString2 = myString2.capitalize()
# Print the original line
(Second original line: %s’ %myString2)
# Print the converted string
(Second converted string: %sn’ %convertedString2)
# Define a line beginning with
myString3 = ‘7827 Ridgeview Court Summerville, SC 29483′.
# String conversion with method
convertedString3 = myString3.capitalize()
# Print original line
( third original line: %s %myString3 )
# Print the converted string
(Third converted string: %sn’ %convertedString3)
A way out:
After executing the script, the following output appears.
Example 2: Use the capitalization method to change each word in a character string to capital letters
The following example shows how the first character of each word in the text can be written with a capital letter. First of all, the value of multiple words is used for the user input. Each text value can be split into a sub-string using the split() method. The split() method is used here to split the text using a space and return the glossary. The variable newString is used here to store the converted text. For the loop, it is used to read each element in the list and the capital letter of each element and store the converted value with a space in newString. The previous value of newString is combined with the new value to achieve the desired result. To see the difference, the source text and the converted text are then printed.
#! /usr/bin/env python3
# Enter text from line
= input (enter text inn)
# Split the text according to the space
strList = text.split()
# Specify the variable that will store the converted string
newString = ”.
# Iterrer la liste
pour fall in strList
# Specify each item in the list and combine
newString += val.capitalize()+ ‘ ‘.
# Print the original string
(The original string is: %s’ %text)
# Print the converted string
(‘Converted string: %sn’ %newString)
A way out:
The next output is that I like Python programming, and after applying the capitalize() and split() methods, I like Python programming to display.
Example 3: The first letter of each sentence is capitalised in the text of a number of sentences.
In the two previous examples, the capize() method is applied to a line of text. But sometimes you have to capitalize the first letter of each line in the file or the uppercase letters of each sentence to work with the contents of the file or the long text of multiple sentences. To solve this problem you can use the capize() method with the split() function. The example shows how to write the first letter of each sentence in capital letters in long texts. Here the variable with the text name is defined with a string value in three sentences. First, the text value is divided by ”. using the split() method to create a list of three sentences. In addition, the capital letter of each record is used for the loop, as in example 2. Here’s . combined with each transformed element to determine the end of the chain. The strip() method is used to remove an extra space and the last extra ‘. is removed from the newText using the position value.
/usr/bin/env python3
# Determine the long text
text = ‘python is a universal, high level interpreted programming language.
by Guido van Rossum. It was first published in 1991.
# Split text according to space
lineList = text.split(”)
# Specify the variable that will store the converted string
newText = ”.
# Iterrer la liste
pour val in lineList
# remove spaces at beginning and end
val = val.strip()
# Write down each item in the list and connect to ”
newText += val.capitalize()+’. ‘
# Delete the last point
newText = newText[:-2].
# Print the original string
(Original text is: n%s’ % text)
# Print the converted string
(nThe converted text is: n%s’ %newText)
A way out:
Both the source text and the converted text are displayed.
Conclusion:
If you are working with string data and need to write the first letter of a string, or the first letter of each word in a string, or the first letter of each sentence in long text, you can use the capize() method with another method to perform the task. The tasks mentioned here are presented in this article with examples. I hope this article will help readers to effectively use the Capitalize() method for string data for many purposes.python capitalize first letter of every word in string,python capitalize every other letter