How to check if type of a variable is string?


Question

Is there a way to check if the type of a variable in python is string. like:

isinstance(x,int);

for integer values?

1
658
7/29/2019 9:18:34 PM

In Python 2.x, you would do

isinstance(s, basestring)

basestring is the abstract superclass of str and unicode. It can be used to test whether an object is an instance of str or unicode.


In Python 3.x, the correct test is

isinstance(s, str)

The bytes class isn't considered a string type in Python 3.

978
10/28/2017 1:59:55 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon