close

How to check if a Character Is a Number in Python

In this tutorial we will learn about How to check if a Character Is a Number in Python. You can check In your input the character you entered is number or not by this method. Lets learn this by given below methods.

How to check if a Character Is a Number in Python

  1. Check if a Character Is a Number in Python

    to check if a Character Is a Number in Python just Use isdigit().By using isdigit() you can check if a Character Is a Number in Python. If your character is a number than isdigit() function return the value is true. And if it is not a number than it will return you false. Thus you can check the character you have been enterd is a number or string or something else.Lets learn this by given below example. It is very simple and easy to use. Lets see it. myvar = "24" x = myvar.isdigit() print(x) myvar2 = "84" y = myvar2.isdigit() print(y) Output : True True

  2. How to check if a Character Is a Number in Python

    to check if a Character Is a Number in Python just Use isnumeric().isnumeric() function can be use to check the character is a number or not in python. You can use it as like as the above method. Here we will use the function isnumeric() instead of isdigit(). If the entered value is a number than this function return tha true value and if not than it will return false just like above method. It is also very simple and easy. Lets learn it by given below example: myvar = "24" x = myvar.isnumeric() print(x) myvar2 = "84" y = myvar2.isnumeric() print(y) Output : True True

Method 1: Use isdigit()

By using isdigit() you can check if a Character Is a Number in Python. If your character is a number than isdigit() function return the value is true. And if it is not a number than it will return you false. Thus you can check the character you have been enterd is a number or string or something else.Lets learn this by given below example. It is very simple and easy to use. Lets see it.

myvar = "24"
x = myvar.isdigit()
print(x)

myvar2 = "84"
y = myvar2.isdigit()
print(y)

Output :

True
True

Method 2: Use isnumeric()

isnumeric() function can be use to check the character is a number or not in python. You can use it as like as the above method. Here we will use the function isnumeric() instead of isdigit(). If the entered value is a number than this function return tha true value and if not than it will return false just like above method. It is also very simple and easy. Lets learn it by given below example:

myvar = "24"
x = myvar.isnumeric()
print(x)

myvar2 = "84"
y = myvar2.isnumeric()
print(y)

Output :

True
True

Method 3: Use if else statements

By using if else statements you can check if a Character Is a Number in Python. This method is little different from above two methods. Here first of all you have to enter an input. And then use if else statement and put the conditions. If the character is number than it print it is a number and not than print it is not a number like below example. Lets learn this by given below example. Here you have to take input from users. Lets see it.

inp = input("Enter the element you want to check:")

if(inp >= '0' and inp <= '9'):
    print("It is a Number")
else:
    print("It is Not a Number")

Output :

Enter the element you want to check:54
It is a Number
Enter the element you want to check:aa
It is Not a Number

Conlusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment