Home >>Python Programs >Python program to check whether a variable is a string or not
In this example, we will see a Python program through which we can check if a given variable is a string or not.
We can check whether a variable is a string or not we can use two different Python library functions:
isinstance() and type()Program 1:
# variables
a = 1000 # an integer variable
b = 10.73 # a float variable
c = 'X' # a character variable
d = 'Hello' # a string variable
e = "Abhimanyu" # a string variable
# checking types
if isinstance(a, str):
print("Variable \'a\' is a type of string.")
else:
print("Variable \'a\' is not a type of string.")
if isinstance(b, str):
print("Variable \'b\' is a type of string.")
else:
print("Variable \'b\' is not a type of string.")
if isinstance(c, str):
print("Variable \'c\' is a type of string.")
else:
print("Variable \'c\' is not a type of string.")
if isinstance(d, str):
print("Variable \'d\' is a type of string.")
else:
print("Variable \'d\' is not a type of string.")
if isinstance(e, str):
print("Variable \'e\' is a type of string.")
else:
print("Variable \'e\' is not a type of string.")
# variables
a = 1000 # an integer variable
b = 10.73 # a float variable
c = 'X' # a character variable
d = 'Hello' # a string variable
e = "Abhimanyu" # a string variable
# checking types
if type(a) == str:
print("Variable \'a\' is a type of string.")
else:
print("Variable \'a\' is not a type of string.")
if type(b) == str:
print("Variable \'b\' is a type of string.")
else:
print("Variable \'b\' is not a type of string.")
if type(c) == str:
print("Variable \'c\' is a type of string.")
else:
print("Variable \'c\' is not a type of string.")
if type(d) == str:
print("Variable \'d\' is a type of string.")
else:
print("Variable \'d\' is not a type of string.")
if type(e) == str:
print("Variable \'e\' is a type of string.")
else:
print("Variable \'e\' is not a type of string.")