Check if a variable is a string using isinstance()

This isinstance(x, str) method can be used to test whether any variable is a particular datatype. By giving the second argument as “str”, we can check if the variable we pass is a string or not. 

Python3




# initializing string
test_string = "GFG"
 
# printing original string
print("The original string : " + str(test_string))
 
# using isinstance()
# Check if variable is string
res = isinstance(test_string, str)
 
# print result
print("Is variable a string ? : " + str(res))


Output:

The original string : GFG
Is variable a string ? : True

Check if a variable is string in Python

While working with different datatypes, we might come across a time, when we need to test the datatype for its nature. This article gives ways to test a variable against the data type using Python. Let’s discuss certain ways how to check variable is a string.

Similar Reads

Check if a variable is a string using isinstance()

This isinstance(x, str) method can be used to test whether any variable is a particular datatype. By giving the second argument as “str”, we can check if the variable we pass is a string or not....

Check if a variable is a string using type()

...