Write a Python program to input a character and to print whether a given character is an alphabet, digit or any other character|Using isaplha() and isdigit() in python|CBSE class 12 program

                     

Introduction

In this python program, we will learn how to check if a given character is an alphabet or a digit or a special character. For this purpose, we are going to use two ways to check if a given character is an alphabet,a digit or a special character. First we will check if the given character is in between a-z or A-Z and 0-9 and the other way to check is by using built-in python functions : isalpha() and isdigit().
 
Following are the source codes and Their Outputs. 

Code 1:-

character = input("Enter any character: ")

if((character >= 'a' and character <= 'z') or (character >= 'A' and character <= 'Z')):

    print("The Given character", character, "is an Alphabet")

elif(character >= '0' and character <= '9'):

    print("The Given character", character, "is a Digit")

else:

    print("The Given character", character, "is a Special character")

Output:-


Executed on IDLE(Python 3.9 64 bit)


Understanding How the above works:-

First the character that we enter gets stored in the variable, named as "character".
Then the program Executes -- if statements --
Now we have to understand that the if statements are based on ASCII value of the characters.That is if the ASCII value of the characters is in the specified range of alphabets then it will print "The Given character is an Alphabet"  and if it is in the range of Number then it will print"The Given character is an Digit".If it is a special character then the "else" statement gets executed.At last WE get the output with the help of "print" statement.


TO UNDERSTAND WORKING OF THE CODE SKIP TO LAST PART.

 Code 2:-

character=input("Enter any character: ")

if character.isalpha()==True:

    print("The character you entered is an alphabet")

elif character.isdigit()==True:

    print("The character you entered is a digit")

else:

    print("You entered a special character")

#---------CODE ENDED---------


Output:-

Executed on Pydroid 3
Executed on Pydroid3


Understanding How the above works:-

 
First the character that we enter gets stored in the variable, named as "character".
Then the program Executes -- if statements --
Next the program checks whether the character is alphabet or a digit with the help of "character.isdigit()" and "character.isalpha()".
If the chracter is alphabet or a number then the print statement under their section executes.If it is a special character then the "else" statement gets executed.At last WE get the output with the help of "print" statement.



"HOPE IT HELPED"

Have anything in mind ?
Let me 
know in the comment section👇
Next Post Previous Post
No Comment
Add Comment
comment url