Check if a string can be converted to an Integer in Python

avatar

Borislav Hadzhiev

Last updated: Jul 13, 2022

banner

Check out my new book

Check if a string can be converted to an Integer in Python #

To check if a string can be converted to an integer:

  1. Wrap the call to the int() class in a try/except block.
  2. If the conversion succeeds, the try block will fully run.
  3. If the conversion to integer fails, handle the ValueError in the except block.
main.py
def can_convert_to_int(string): try: int(string) return True except ValueError: return False print(can_convert_to_int('1357')) # 👉️ True print(can_convert_to_int('-1357')) # 👉️ True print(can_convert_to_int('1.357')) # 👉️ False print(can_convert_to_int('ABC123')) # 👉️ False

We used a try/except block to check if a string can be converted to an integer.

If the try block succeeds, the function returns True and the string can be converted to an integer.

If calling the int() class with the string fails, a ValueError is raised and the function returns False.

Using a try/except statement in this manner is commonly known as "asking for forgiveness rather than permission".

We pass the string to the int() class not knowing whether the conversion will be successful, and if a ValueError error is raised, we handle it in the except block.

Alternatively, you can use the str.isdigit() method.

Check if string can be converted to Integer using isdigit() #

Use the str.isdigit() method to check if a string can be converted to an integer, e.g. if my_str.isdigit():. If the str.isdigit method returns True, then all of the characters in the string are digits and it can be converted to an integer.

main.py
my_str = '1357' if my_str.isdigit(): my_int = int(my_str) print(my_int) # 👉️ 1357 print('✅ string can be converted to integer') else: print('⛔️ string CANNOT be converted to integer')

We used the str.isdigit() method to check if a string can safely be converted to an integer.

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

The method checks if all characters in the string are digits, so if the string had a minus sign or a decimal, it would return False.

main.py
my_str = '-1357' if my_str.isdigit(): my_int = int(my_str) print(my_int) print('✅ string can be converted to integer') else: # 👇️ this runs print('⛔️ string CANNOT be converted to integer')
If your string might be a negative integer, use a try/except example instead, as it also covers negative integers.

If you just want to try converting the string to an integer and silence/ignore the ValueError if the conversion fails, use a pass statement.

main.py
my_str = '2468' try: my_int = int(my_str) print(my_int) # 👉️ 2468 except ValueError: pass

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

main.py
class Employee: pass
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.