Handling ValueError in Python: Detecting Strings and Integers

Python Detect String And Int Using Exception Handling

When you try to use operators on the wrong operands, Python will automatically raise an error and your program will come to a halt. Errors are also raised when we are unable to detect the type of variables before performing an operation.

Similarly, ValueErrors are raised when a function call is correct but the argument or parameter passed is wrong. When incorrect values are assigned to correct functions, Python also raises ValueError.

Exception handling is a method of handling errors in python. It interrupts the normal flow of a program. Exception handling can be manually done using the try: and except: blocks in code.

Without proper detection of strings and integers, the most common error that is raised is ValueError .

Understanding Errors and Exceptions in Python

ValueErrors is a type of exception in Python, which is detected during the runtime of a program. Exceptions are raised when there is an error in the program and we try to run the code as it is without handling it. Most exceptions in Python are not handled by itself, instead, we need to use a special type of code block.

Handling Exceptions with try and except Blocks

Handling ValueError in Python involves detecting strings and integers using exception handling. ValueError occurs when a function receives an argument of the wrong type. To handle these errors, use try and except clauses in your code. This method helps improve your code’s robustness and allows for more graceful error handling.

The try and except block works in the following way:

  • First, the try clause is executed.
  • If during the execution of the code under the try clause raises an error, the code inside the except clause is executed.
  • If there are no exceptions raised then the execution of the code under the try clause is executed and then the program is run as usual.

Similar: Python Attribute Error – (Solved)

Differences between Integers and Strings in Python

Integers are a type of variable that contain whole numbers such as 1,2,50,60…etc . Whereas, strings are a sequence of characters enclosed within double(“”) quotes or single quotes(”). The characters can be anything from letters to symbols to even numbers.

In Python, there is an in-built function called int() which explicitly converts other types of variables such as floats, or strings into whole numbers.

In case of strings, only those strings can be converted into integers that contain numbers from 0 to 9 inside double or single quotes. English letters or special symbols cannot be converted into integers hence raising a ValueError.

Example of a ValueError in Python

In the code block in the picture below, a string is passed instead of an integer while calculating its square root using the math.sqrt() function. For example, if I try to convert “abc” explicitly into an integer, then try to get the square root of it, I will inevitably face a ValueError as shown below.

ValueError In Python
ValueError In Python

Implementing Exception Handling for ValueError

Let’s look at how we can use the try and except clauses, to handle ValueErrors.

Let’s implement the try and except clauses for ValueError handling.

# importing required module
import math

var = input("Enter number= ")
# exception handling
# try clause
    print(math.sqrt(int(var)), end=" This is the square root of the given number!")
# except clause
except ValueError:
    print("Please enter numerical values only! Not alphabets!")

Now, if we use the values from the previous section, that is abc, this is the output we get:

Enter number= abc
Please enter numerical values only! Not alphabets
Exception Handling In Python 1
Exception Handling In Python

Instead, if we enter a proper input, like 16, we get the output as follows:

Enter number= 16
4.0 This is the square root of the given number!
Exception Handling In Python
Exception Handling In Python

Do check out: Python String contains: Check if String Contains Substring.


In this article, we’ve explored exception handling in Python, focusing on handling ValueErrors. These errors occur when a function receives an argument of the wrong type. By using try and except clauses, we can gracefully handle these errors and improve our code’s robustness. As a programmer, how can you apply exception handling to enhance your projects and make them more error-resistant?