How to compare one string with an integer value in python

How to compare one string with numbers in Python:

Both string and number are of different data types. We can compare two strings or two integers. Python uses lexicographic ordering for strings and numeric ordering for integers.

But, how can we compare one string with a numeric value in Python? We can do that by converting the string value to an integer or float. The conversion of one data type to another is called type casting. The values can be compared after the conversion. In this post, I will show you how to compare a string value with an integer or float.

Can we compare a string with a number without type-casting:

If we try to compare one string with a number, it will throw one TypeError. For example:

given_str = '5'
given_int = 10

print(given_int > given_str)

It will print:

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    print(given_int > given_str)
TypeError: '>' not supported between instances of 'int' and 'str'

Python error string int compared

We need to use type casting to change the variables either to string or integer before we make any comparison.

Example 1: Python program to compare string and integer:

We can use the int() method to convert an integer to a string. The following program shows how it works:

str_first = '5'
int_first = 10
int_second = 5

print(int_first > int(str_first))
print(int_first == int(str_first))
print(int_second == int(str_first))

Download it on GitHub

It will print:

True
False
True

It converted the string str_first to an integer and compared it with the other integer values. We can also convert the integer values to string values with the str() constructor.

str_first = "5"
int_first = 10
int_second = 5

print(str(int_first) > str_first)
print(str(int_first) == str_first)
print(str(int_second) == str_first)

Download it on GitHub

It will print:

False
False
True

We can’t use comparison like greater than or less than but we can use the equality operator, == if we convert the integer value to a string.

Example 2: Python program to compare string and float:

Similar to the previous example, we can compare a string with a float value by type-casting the string to float with the float() method. The following example shows how to compare string values with float:

str_first = "5.13"
int_first = 10.34
int_second = 5.13

print(int_first > float(str_first))
print(int_first == float(str_first))
print(int_second == float(str_first))

Download it on GitHub

It will print:

True
False
True

We can also convert the float values to string values and compare these values.

str_first = "5.13"
int_first = 10.34
int_second = 5.13

print(str(int_first) > str_first)
print(str(int_first) == str_first)
print(str(int_second) == str_first)

Download it on GitHub

It will print:

False
False
True

Handling Exception with invalid values:

For invalid inputs, it will throw one error. For example,

given_str = '5xx'
given_int = 10

print(given_int > int(given_str))

Here, the variable given_str is not a valid integer value. So, it will throw one error:

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    print(given_int > int(given_str))
ValueError: invalid literal for int() with base 10: '5xx'

We can use a try-except block to handle the exception:

given_str = "5xx"
given_int = 10

try:
    print(given_int > int(given_str))
except:
    print("Exception thrown on int conversion")

Download it on GitHub

You might also like: