Home » Python » Python programs

Python program to check whether a variable is a string or not

Python | Check if a variable is a string: Here, we are going to learn how to check whether a given variable is a string type or not in Python programming language?
Submitted by IncludeHelp, on April 13, 2020

Python | Check if a variable is a string

To check whether a defined variable is a string type or not, we can use two functions which are Python library functions,

  1. Using isinstance()
  2. Using type()

1) Checking a variable is a string or not using isinstance() function

isinstance() function accepts two parameters – 1) variable name (object) and 2) data type (class) and returns whether an object is an instance of a class or of a subclass thereof.

Syntax:

    isinstance(obj, class_or_tuple)

Example:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # 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.")

Output

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.

2) Checking a variable is string using type() function

type() function accepts one parameter (others are optional), and returns its type.

Syntax:

    type(object)

Example:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # 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.")

Output

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.