Skip to content

Python check if variable is string | Example code

  • by

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 a variable, and the second is the 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.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading