Reverse case program of any alphabet in Python
In this tutorial, you are going to learn the reverse case program of any alphabet in Python. For understanding this program you should know that what is a reverse case of any alphabet in Python. Reverse case of any alphabet means that to convert any capital alphabet into small alphabet or small alphabet into capital alphabet. For this conversion, we will need the ASCII characters and their values.
ASCII stands for American Standard Code for Information Interchange. There are a total of 128 characters, with values from 0 to 127. The ASCII characters include capital letters, small letters, digits and special symbols. All the characters have a different range of values. The ASCII values of the capital letter are from 65-90, small letters are from 97 -122, numbers are from 48-57 and special symbols are from 0-47, 58-64, 91- 96, 123- 127.
ASCII characters ASCII values
Capital letters (A-Z) [65-90]
Small letters (a-z) [97-122]
Digits (0-9) [48-57]
Special symbols [0-47, 58-64, 91-96, 123-127]
To convert ASCII characters into ASCII values and vice-versa, we should know about the functions which can do this conversion. ord() and chr() functions are used for this conversion.
Ord() : This function is used to convert the ASCII characters to ASCII values.
ch= 'A' # ord() to get the ASCII value print('The ASCII value is:', ord(ch))
The ASCII value is: 65
Chr() : This function is used to convert the ASCII values to ASCII characters.
ch= 107 # chr() to get the ASCII character c=chr(ch) print('The ASCII character is:', c)
The ASCII character is: k
Reverse case program of any alphabet
- Input any alphabet in ch.
- ord() function will convert the character in ch into the corresponding ASCII value.
- If the ASCII value range between 65-90 then it is a capital letter. Add 32 into the ASCII value to convert it into the corresponding small letter’s ASCII value.
- If the above condition is not true then the entered character is a small letter. Convert the small letter’s ASCII value into capital letter’s ASCII value by subtracting 32 from it.
- Convert the ASCII value into the character through chr() function.
ch=input("Enter any alphabet is :") # to convert the input character into the ASCII value ch=ord(ch) # to check whether the input character is a capital or small letter if ch>=65 and ch<=90: ch=ch+32 else: ch=ch-32 # to convert the ASCII value into the character c=chr(ch) print("Reverse case alphabet is :",a)
Enter any alphabet is : h Reverse case alphabet is : H
Note: The input character is a capital letter or small letter or a digit or a special character. You can use the same concept of ASCII value as the loop condition in a program to know about the input character.
Go and check other tutorials on python: