Make all characters of string capital in Python
In this tutorial, we will learn how to make all characters of string capital in Python. In order to make all the characters of a string into the capital, we will use the Python built-in string function i.e., upper()
.
String upper() method in Python
The upper()
method is a built-in Python string method. This function generally converts all the lowercase characters into uppercase characters.
Syntax :
anystring.upper()
Where the anystring
refers to the string which should be converted into uppercase.
The upper() function does not house(hold) any parameters.
Returns :
The upper()
function returns the uppercased version of all the characters.
So now let us understand the upper()
function in detail with a few sets of examples.
Example 1: For all alphabetic characters
# Alphabetic characters s1='codespeedy' s2=s1.upper() print('Previous String:' +s1) print('New String:' +s2)
Here, the code shows two views of a string. Where initially the string s1 is initialized to lowercase characters. And by the use of the upper()
function, we assigned the new string to s2 having all the uppercase characters in it.
Therefore above program displays the output as,
Previous String: codespeedy New String: CODESPEEDY
The output of the code displays two strings. First, the previous string contains all lowercase characters. Second, the new string contains all uppercase characters.
Example 2: For alphanumeric characters
The alphanumeric string refers to having both alphabets(a..z, A..Z) and numbers(0…9) in a string.
# Alphanumeric string s3= '123code456speedy' print(s3.upper()) s4= 'a1b2c3d4e5' print(s4.upper())
The above code contains alphanumeric strings. It converts all the lowercase characters into uppercase and leaves all the digits in their places. Therefore, the output of the above code is,
123CODE456SPEEDY A1B2C3D4E5
Hence, it displays two resultant strings having all lowercase characters converted into uppercase characters.
Example 3: Used for assigning and checking
str ='hello' str1 =str.upper() print(str1) if str1 == 'hello'.upper(): p='true' else: p='false' print(p)
The above code is used to assign as well as for checking. For instance, variable str1 is having the capital version of the string str. And has been examined at the if loop. The output of the above code is,
HELLO true
Therefore, the code displays the first time the capital version of the string. And next time true statement.
Recommended blogs :
Leave a Reply