Python Check if String is Integer

0
72

In this article, we will write a program in python that will check if a string is an integer or not.

We will create a function that will take a string as input and returns a Boolean value (true or false) based on the condition if it is an integer or not. We will take the input from the user and call the function. After storing the result, we will print on the console whether the string is an integer or not.

If all the characters of an inputted string are between ‘0’ and ‘9’, then it means that the string is an integer.

For Example:

123, It is an Integer because all the characters are between 0 and 9.

35446554667876832, It is also an Integer because all the characters are between 0 and 9 only.

234t5, It is not an Integer because it contains a character ‘t’ which is not between 0 and 9.

If any character is not following the rule, then we can consider that the string is not an Integer.

Testcase 1: When the input is 123.

Output:

Testcase 2: When the input is 236r.

Output:

If all the characters of a string are between 0 and 9 then we can say that the string is an integer otherwise it is not considered an integer. We have seen the program for the same where we created a function that takes a string as input and tells us if the string is an integer or not.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here