Python Program to convert string to Opposite case

By | August 16, 2019

Task: Write a Python Program to convert string to Opposite case. This Python programming tutorial will input a string and convert its letters to opposite case. It means that if this Python code will find a lower case letter, it will convert it into upper case and vice versa.

For example, it will convert the input string Hello into hELLO. It converts ‘H’ into ‘h’, ‘e’ into E and so on.

The Source code of Python Program to convert string to Opposite case

# Write a Python program to
# input a string and change its
# case(opposite case) using swapcase()
# function convert lower case letter to
# upper case and vice versa.
# The user will enter String
# during program execution

# display message to enter string
# and get input string
str1 = input("Enter a string to convert into opposite case letter wise:")


# use lower() method
opposite_string = str1.swapcase()

# now print UPPER CASE string
print(str1, "in oPPOSITE case =", opposite_string)

The output of the Python program

Enter a string to convert into opposite case letter wise:Hello python
Hello python in oPPOSITE case = hELLO PYTHON

Leave a Reply

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