In this article, we will discuss different ways to check if a string contains an integer value or not in Python.
Table of Contents
- Check if String contains an Integer only using Regex
- Check if String contains an Integer only using isnumeric()
- Check if String contains an Integer only using Exception handling
- Check if String contains an Integer only using isdigit()
- Check if String contains an Integer only using all() & map()
Check if String contains an Integer only using Regex
Make a regex pattern to check that,
- String starts with a digit or a symbol ‘+’ or ‘-‘.
- After that string should contain only digits till end.
Then using the regex.search() function, check if the whole string matches the given regex pattern or not. We have created a function, that uses regex and checks if given string contains an integer only,
import re def is_integer_only(sample_str): ''' Returns True if given string contains a positive or negative integer only ''' result = re.match("[-+]?\d+$", sample_str) return result
It will return True, if the given string contain a positive or negative integer only. Now let’s use this,
Example 1:
sample_str = "1024" if is_integer_only(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain an integer only' % (sample_str))
Output:
Frequently Asked:
- Check if String contains an element from List in Python
- Check if String Ends With a Number in PHP
- Split a string into a list of characters in Python
- How to convert a string to bytes in Python?
Yes, string 1024 contains an integer only
All the characters in the string were integers, therefore is_integer_only() returned True.
Example 2: What if the given string contains a dot along with numbers i.e. a float?
sample_str = "1024.23" if is_integer_only(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string 1024.23 does not contain only integer
All the characters in the string were not integers i.e. a dot symbol was also there. Therefore is_integer_only() returned False. So, this shows that is_integer_only() does not work for strings containing floats, instead it works only for integers.
Example 3: What if the given string contains a negative number?
sample_str = "-1024" if is_integer_only(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
Yes, string -1024 contains an integer only
The string started with a minus symbol and then after that all the characters in the string were integers, therefore is_integer_only() returned True.
Example 4: What if the string is alpha numeric?
sample_str = "abc1024sf" if is_integer_only(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string abc1024sf does not contain only integer
All the characters in the string were not integers i.e. there were some alphabets too in the string. Therefore is_integer_only() returned False. So, this shows that is_integer_only() returns False for alphanumeric strings.
Check if String contains an Integer only using isnumeric()
In Python, the string class provides a member function isnumeric(), which returns True if all the characters in calling string object are numeric i.e. 0 to 9. We can use that to check if a given string contains only an integer or not. Let’s see some examples,
Example 1:
sample_str = "1024" if sample_str.isnumeric(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain an integer only' % (sample_str))
Output:
Yes, string 1024 contains an integer only
All the characters in the string were integers, therefore isnumeric() returned True.
Example 2: What if the given string contains a dot along with numbers i.e. a float?
sample_str = "1024.19" if sample_str.isnumeric(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain an integer only' % (sample_str))
Output:
No, string 1024.19 does not contain an integer only
All the characters in the string were not integers i.e. a dot symbol was also there. Therefore isnumeric() returned False. So, this shows that str.isnumeric() does not work for strings containing floats, instead it works only for integers.
Example 3: What if the given string contains a negative number?
sample_str = "-1024" if sample_str.isnumeric(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string -1024 does not contain only integer
All the characters in the string were not integers i.e. a minus symbol was also there. Therefore isnumeric() returned False. So, this shows that str.isnumeric() does not work for strings containing negative integers.
Example 4: What if the string is alpha numeric?
sample_str = "abc1024sf" if sample_str.isnumeric(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string abc1024sf does not contain only integer
All the characters in the string were not integers i.e. there were some alphabets too in the string. Therefore isnumeric() returned False. So, this shows that str.isnumeric() returns False for alphanumeric strings.
Check if String contains an Integer only using Exception handling
We can cast the string to integer by using int(str) function. But if the string contains anything other than digits then it will raise an error i.e. ValueError. We have created a function that will use try/except to check if strings contains only an integer or not i.e.
def is_integer(sample_str): ''' Returns True if given string contains a positive or negative integer only ''' result = True try: int(sample_str) except: result = False return result
If the given string contains any thing other then integer then int() will raise ValueError and in that case this function will return False, otherwise it will True. Now let’s use this function to check if a string contains only an integer,
Example 1:
sample_str = "1024" if is_integer(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain an integer only' % (sample_str))
Output:
Yes, string 1024 contains an integer only
All the characters in the string were integers, therefore is_integer() function returned True.
Example 2: What if the given string contains a dot along with numbers i.e. a float?
sample_str = "1024.23" if is_integer(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain an integer only' % (sample_str))
Output:
No, string 1024.23 does not contain an integer only
All the characters in the string were not integers i.e. a dot symbol was also there. Therefore is_integer() returned False. So, this shows that is_integer() does not work for strings containing floats, instead it works only for integers.
Example 3: What if the given string contains a negative number?
sample_str = "-1024" if is_integer(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
Yes, string -1024 contains an integer only
Using int() function works fine with string containing a negative integer, therefore is_integer() function returned True.
Example 4: What if the string is alpha numeric?
sample_str = "abc1024sf" if is_integer(sample_str): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string abc1024sf does not contain only integer
All the characters in the string were not integers i.e. there were some alphabets too in the string. Therefore is_integer() returned False. So, this shows that is_integer() returns False for alphanumeric strings.
Check if String contains an Integer only using isdigit()
In Python, string class proides a member function isdigit(), which returns True if the given string contains only digits, False otherwise.
Let’s use this str.isdigit() to check if string contains an integer only,
Example 1:
sample_str = "1024" if sample_str.isdigit(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain an integer only' % (sample_str))
Output:
Yes, string 1024 contains an integer only
All the characters in the string were integers, therefore isdigit() returned True.
Example 2: What if the given string contains a dot along with numbers i.e. a float?
sample_str = "1024.23" if sample_str.isdigit(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string 1024.23 does not contain only integer
All the characters in the string were not integers i.e. a dot symbol was also there. Therefore isdigit() returned False. So, this shows that str.isdigit() does not work for strings containing floats, instead it works only for integers.
Example 3: What if the given string contains a negative number?
sample_str = "-1024" if sample_str.isdigit(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string -1024 does not contain only integer
All the characters in the string were not integers i.e. a minus symbol was also there. Therefore isdigit() returned False. So, this shows that str.isdigit() does not work for strings containing negative integers.
Example 4: What if the string is alpha numeric?
sample_str = "abc1024sf" if sample_str.isdigit(): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string abc1024sf does not contain only integer
All the characters in the string were not integers i.e. there were some alphabets too in the string. Therefore isdigit() returned False. So, this shows that str.isdigit() returns False for alphanumeric strings.
Check if String contains an Integer only using all() & map()
Iterate over all the characters in string call the isdigit() function on each of the character using map() function. It returnes a sequence of boolean values, where each True value corresponds to a digit and False for anything other than digit. Then pass that boolean sequence to all() function, it will return True if all entries in that sequence is True i.e. it means if all characters in string are digits only. Let’s see some examples,
Example 1:
sample_str = "1024" if all(map(str.isdigit, sample_str)): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain an integer only' % (sample_str))
Output
Yes, string 1024 contains an integer only
All the characters in the string were integers, therefore it returned True.
Example 2: What if the given string contains a dot along with numbers i.e. a float?
sample_str = "1024.23" if all(map(str.isdigit, sample_str)): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string 1024.23 does not contain only integer
All the characters in the string were not integers i.e. a dot symbol was also there. Therefore it returned False.
Example 3: What if the given string contains a negative number?
sample_str = "-1024" if all(map(str.isdigit, sample_str)): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string -1024 does not contain only integer
All the characters in the string were not integers i.e. a minus symbol was also there. Therefore it returned False.
Example 4: What if the string is alpha numeric?
sample_str = "abc1024sf" if all(map(str.isdigit, sample_str)): print(f'Yes, string %s contains an integer only' % (sample_str)) else: print(f'No, string %s does not contain only integer' % (sample_str))
Output:
No, string abc1024sf does not contain only integer
All the characters in the string were not integers i.e. there were some alphabets too in the string. Therefore it returned False.
Summary
We learned about different ways to check if a string contains only a integer value or not.