Python check if variable is string | Example code

Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the second is the type you want to check for.

isinstance(Var, Data_Type)

Python checks if the variable is a string example

Simple example code, The first parameter will be given variable, and the second is data type string.

var = "String"

print(isinstance(var, str))

Output:

Python check if variable is string

Another Example

Uses the type built-in function to see if the type of your variable is str.

var = "String"

if type(var) == str:
    print("String type")

Output: String type

Do comment if you have any questions or suggestions on this Python var string topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.