How to Check If Variable is String in Python

Python is a dynamically typed language that presents many programmatical errors. In some cases, we don’t have any assurance that the passed variable to a method is the type we’re expecting it to be in. However, before performing any operation, it is necessary to check whether the variable is of a particular data type. Today’s post will discuss how to check if a variable is a string and explore different ways to do it.

How to Check if String Python

  • By using type() function with str class.
  • By using isinstance() method.
  • By using is Operator.

To check if a variable is a String in Python, use the type() function and compare the output of the type() function to the str class, and if it returns True, then a variable is String; otherwise not. The type() is a built-in Python function that takes a variable as input and returns its data type.

tring = "PythonSolved"
print("The original string : " + str(tring))
op = type(tring) == str
print("Is variable a string: " + str(op))

Output

The original string : PythonSolved
Is variable a string: True

You can see that we get True, which means the variable is a string. Now, let’s set a variable to an integer and see the output.

tring = 22
print("The original string : " + str(tring))
op = type(tring) == str
print("Is variable a string: " + str(op))

Output

The original string : 22
Is variable a string: False

You can see that 22 is a numeric value that is not a string, and hence it returns False.

Checking Python String using isinstance() method

Python isinstance() is a built-in method that tests whether any variable is a specific datatype. The isinstance() method takes two arguments: variable and data type in which we need to check against.

The isinstance() method returns boolean value. If the data type matches, it returns True otherwise, False.

tring = "PythonSolved"
print("The original string : " + str(tring))
op = isinstance(tring, str)
print("Is variable a string: " + str(op))

Output

The original string : PythonSolved
Is variable a string: True

In this example, we declared a String variable whose value is “PythonSolved”. Then, we pass the first argument as that variable to the isinstance() and data type str as the second argument. Since it is a string, it returns True, but if we pass integers or numbers, it will return False.

tring = 1921
print("The original string : " + str(tring))
op = isinstance(tring, str)
print("Is variable a string: " + str(op))

Output

The original string : 1921
Is variable a string: False

If you pass integers as a string, it will return True again.

tring = "1921"
print("The original string : " + str(tring))
op = isinstance(tring, str)
print("Is variable a string: " + str(op))

Output

The original string : 1921
Is variable a string: True

Check if Variable is a String using is Operator

The is Operator is a built-in Python operator that checks if two compared variables point to the exact memory location. We can compare the result of the type() function with the str class using is Operator, and if it returns True, then a variable is String; otherwise not.

tring = "PythonSolved"
print("The original string : " + str(tring))
if (type(tring)) is str:
   print("It is a string")
else:
   print("It is not a string")

Output

The original string : PythonSolved
It is a string

And we got the accurate result. Now, let’s check with an integer.

tring = 21
print("The original string : " + str(tring))
if (type(tring)) is str:
   print("It is a string")
else:
   print("It is not a string")

Output

The original string : 21
It is not a string

As expected, we get that it is not a string.

Conclusion

Checking a variable is a string is not tricky, and we have seen three ways you can use it to check it properly. In my opinion, if you use the isinstance() method, it is more efficient. Otherwise, you can with any other approaches mentioned in this article.

More posts

Check if array is empty

Check if the dictionary has a key

Python syntax checker

Leave a Comment