On this article, we’ll write a program in python that can verify if a string is an integer or not.
We are going to create a perform that can take a string as enter and returns a Boolean worth (true or false) based mostly on the situation whether it is an integer or not. We are going to take the enter from the consumer and name the perform. After storing the consequence, we’ll print on the console whether or not the string is an integer or not.
If all of the characters of an inputted string are between ‘0’ and ‘9’, then it signifies that the string is an integer.
For Instance:
123, It’s an Integer as a result of all of the characters are between 0 and 9.
35446554667876832, Additionally it is an Integer as a result of all of the characters are between 0 and 9 solely.
234t5, It isn’t an Integer as a result of it comprises a personality ‘t’ which isn’t between 0 and 9.
If any character will not be following the rule, then we will take into account that the string will not be an Integer.
return True
# Take string as enter from the consumer
str1 = enter(“Enter a String : “)
# Name the perform and retailer the consequence
consequence = checkInteger(str1)
# If result’s True then print that it’s an integer
# Else print that it’s not an Integer
if (consequence):
print(f”{str1} is an Integer”)
else:
print(f”{str1} will not be an Integer”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# Operate to verify if a string is Integer of Not def checkInteger(str1):     “”“     Operate to returns if a string is Integer or Not     Enter : String worth     Output : Boolean Worth ( True or False )     ““”     size = len(str1) # Discover the size of the string     # loop from 0 to size – 1     for i in vary(0, size):         # if the character will not be higher than 0 and fewer than 9         # it signifies that the string comprises any alphabetic character         # so it’s not an integer. On this case return False         # in any other case return True after checking all of the characters         if (not (str1[i] >= ‘0’ and str1[i] <= ‘9’)):             return False      return True  # Take string as enter from the consumer str1 = enter(“Enter a String : “)  # Name the perform and retailer the consequence consequence = checkInteger(str1)  # If result’s True then print that it’s an integer # Else print that it’s not an Integer if (consequence):     print(f“{str1} is an Integer”) else:     print(f“{str1} will not be an Integer”)  |
Testcase 1: When the enter is 123.
Output:
PS C : Customers ASUS Desktop Loopy Programmer Work > python –u ” c : Customers ASUS Desktop Loopy Programmer Work check.py “ Enter a String : 123 123 is an Integer |
Testcase 2: When the enter is 236r.
Output:
PS C : Customers ASUS Desktop Loopy Programmer Work > python –u ” c : Customers ASUS Desktop Loopy Programmer Work check.py “ Enter a String : 236r 236r is not an Integer  |
If all of the characters of a string are between 0 and 9 then we will say that the string is an integer in any other case it’s not thought-about an integer. We’ve got seen this system for a similar the place we created a perform that takes a string as enter and tells us if the string is an integer or not.