How to Check if a Variable is a String - Python
Last Updated :
18 Apr, 2025
The goal is to check if a variable is a string in Python to ensure it can be handled as text. Since Python variables can store different types of data such as numbers, lists or text, it’s important to confirm the type before performing operations meant only for strings. For example, if a variable contains the value "hello", we may want to check whether it is a string or not. Let's explore different methods to do this efficiently.
Using isinstance()
isinstance() is the most efficient way to check if a variable is of a specific type. It supports inheritance, meaning it will return True even if the variable is an instance of a subclass of str.
Python
a = "hello"
if isinstance(a, str):
print("Yes")
else:
print("No")
Explanation: isinstance() function checks if a is an instance of the str class or its subclass. If true, it prints "Yes", otherwise, it prints "No".
Using type()
type() method checks the exact type of a variable. It returns True only if the variable is strictly a string (str) and not a subclass. It's simple and works well when you want a strict match.
Python
a = "hello"
if type(a) == str:
print("Yes")
else:
print("No")
Explanation: type() function checks if a is exactly of the str type. If true, it prints "Yes" otherwise, it prints "No".
Using try- except
Instead of checking the type directly, this method tries to use a string method like .lower() and sees if it works. If it fails, Python will raise an AttributeError, meaning the variable is not a string. This follows Python’s “try it and see” style and also called duck typing.
Python
a = "hello"
try:
a.lower()
print("Yes")
except AttributeError:
print("No")
Explanation: This code attempts to call the lower() method on a. If a is a string, the method will work, and it prints "Yes". If a does not have a lower() method (e.g., it's not a string), it raises an AttributeError, and the code prints "No".
Using re module
This way uses regular expressions to check if the variable is a string and optionally matches a specific pattern. It’s a bit of an overkill just for type checking but can be helpful when you also want to validate the content of the string.
Python
import re
a = "hello"
if isinstance(a, str) and re.fullmatch(r".*", a):
print("Yes")
else:
print("No")
Explanation: If a is a string using isinstance(a, str). If true, it then uses re.fullmatch(r".*", a) to confirm that a matches the pattern for any string since .* matches any string .
Similar Reads
How To Check If Variable Is Empty In Python? Handling empty variables is a common task in programming, and Python provides several approaches to determine if a variable is empty. Whether you are working with strings, lists, or any other data type, understanding these methods can help you write more robust and readable code. In this article, we
2 min read
Python - Check if variable is tuple We are given a variable, and our task is to check whether it is a tuple. For example, if the variable is (1, 2, 3), it is a tuple, so the output should be True. If the variable is not a tuple, the output should be False.Using isinstance()isinstance() function is the most common and Pythonic way to c
2 min read
Python | Check if string is a valid identifier Given a string, write a Python program to check if it is a valid identifier or not. An identifier must begin with either an alphabet or underscore, it can not begin with a digit or any other special character, moreover, digits can come after gfg : valid identifier 123 : invalid identifier _abc12 : v
3 min read
How to change any data type into a String in Python? In Python, it's common to convert various data types into strings for display or logging purposes. In this article, we will discuss How to change any data type into a string. Using str() Functionstr() function is used to convert most Python data types into a human-readable string format. It is the m
2 min read
How To Check If A String Can Be Converted To Float In Python? In Python, determining whether a string can be successfully converted to a float is a common task, especially when dealing with user input or external data. This article explores various methods to check if a string is convertible to a float, providing insights into handling potential exceptions and
3 min read