In the previous article, we have discussed Python String lower() Method with Examples
String upper() Method in Python:
The upper() method returns a string in which all characters are capitalized(upper case).
Symbols and numbers are not accepted.
Syntax:
string.upper()
Parameter Values: This method has no parameters.
Return Value:
upper() returns the uppercase string from a given string.
All lowercase characters are converted to uppercase.
If no lowercase characters are found, the original string is returned.
Examples:
Example1:
Input:
Given String = "hello THIS iS btechgeeks"
Output:
The given original string : hello THIS iS btechgeeks The given string after applying upper() method : HELLO THIS IS BTECHGEEKS
Example2:
Input:
Given String = "gooD mornING btechgeeks @!@#123"
Output:
The given original string : gooD mornING btechgeeks @!@#123 The given string after applying upper() method : GOOD MORNING BTECHGEEKS @!@#123
String upper() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Apply the upper() method to the given string that returns a string with all characters in the upper case.
- Store it in another variable.
- Print the given original string.
- Print the given string after applying the upper () method.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "hello THIS iS btechgeeks" # Apply upper() method to the given string that returns a string with all # characters in upper case. # Store it in another variable. rslt = gvn_str.upper() # Print the given original string. print("The given original string :", gvn_str) # Print the given string after applying the upper() method. print("The given string after applying upper() method :") print(rslt)
Output:
The given original string : hello THIS iS btechgeeks The given string after applying upper() method : HELLO THIS IS BTECHGEEKS
How does upper() function work in a program?
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Converting both the given first and second strings into uppercase using the upper() method and checking if both the strings are the same or not using the if conditional statement.
- If it is true, then print “The given first and second strings are same”.
- Else print “The given first and second strings are NOT same”.
- The Exit of the program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "WELCOME to python PROGRAMS" # Give the second string as static input and store it in a variable. gvn_scndstr = "Welcome to PYTHON programs" # Converting both the first and second strings into uppercase using the upper() # method and checking if both the strings are same or not using the if conditional # statement. if(gvn_fststr.upper() == gvn_scndstr.upper()): # If it is true, then print "The given first and second strings are same". print("The given first and second strings are same") else: # Else print "The given first and second strings are NOT same". print("The given first and second strings are NOT same")
Output:
The given first and second strings are same
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Apply the upper() method to the given string that returns a string with all characters in the upper case.
- Store it in another variable.
- Print the given original string.
- Print the given string after applying the upper () method.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some Random String = ") # Apply upper() method to the given string that returns a string with all # characters in upper case. # Store it in another variable. rslt = gvn_str.upper() # Print the given original string. print("The given original string :", gvn_str) # Print the given string after applying the upper() method. print("The given string after applying upper() method :") print(rslt)
Output:
Enter some Random String = gooD mornING btechgeeks @!@#123 The given original string : gooD mornING btechgeeks @!@#123 The given string after applying upper() method : GOOD MORNING BTECHGEEKS @!@#123
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.