Is there a way to check if the type of a variable in python is a string
, like:
isinstance(x,int);
for integer values?
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.
I know this is an old topic, but being the first one shown on google and given that I don't find any of the answers satisfactory, I'll leave this here for future reference:
six is a Python 2 and 3 compatibility library which already covers this issue. You can then do something like this:
import six
if isinstance(value, six.string_types):
pass # It's a string !!
Inspecting the code, this is what you find:
import sys
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str,
else:
string_types = basestring,
The type module also exists if you are checking more than ints and strings. http://docs.python.org/library/types.html
since basestring
isn't defined in Python3, this little trick might help to make the code compatible:
try: # check whether python knows about 'basestring'
basestring
except NameError: # no, it doesn't (it's Python3); use 'str' instead
basestring=str
after that you can run the following test on both Python2 and Python3
isinstance(myvar, basestring)
Lots of good suggestions provided by others here, but I don't see a good cross-platform summary. The following should be a good drop in for any Python program:
def isstring(s):
# if we use Python 3
if (sys.version_info[0] >= 3):
return isinstance(s, str)
# we use Python 2
return isinstance(s, basestring)
In this function, we use isinstance(object, classinfo)
to see if our input is a str
in Python 3 or a basestring
in Python 2.
Here is my answer to support both Python 2 and Python 3 along with these requirements:
six
or similar compat module as they tend to hide away what
is trying to be achieved.import sys
PY2 = sys.version_info.major == 2
# Check if string (lenient for byte-strings on Py2):
isinstance('abc', basestring if PY2 else str)
# Check if strictly a string (unicode-string):
isinstance('abc', unicode if PY2 else str)
# Check if either string (unicode-string) or byte-string:
isinstance('abc', basestring if PY2 else (str, bytes))
# Check for byte-string (Py3 and Py2.7):
isinstance('abc', bytes)
If you do not want to depend on external libs, this works both for Python 2.7+ and Python 3 (http://ideone.com/uB4Kdc):
# your code goes here
s = ["test"];
#s = "test";
isString = False;
if(isinstance(s, str)):
isString = True;
try:
if(isinstance(s, basestring)):
isString = True;
except NameError:
pass;
if(isString):
print("String");
else:
print("Not String");
You can simply use the isinstance function to make sure that the input data is of format string or unicode. Below examples will help you to understand easily.
>>> isinstance('my string', str)
True
>>> isinstance(12, str)
False
>>> isinstance('my string', unicode)
False
>>> isinstance(u'my string', unicode)
True
isinstance(True, int) is True
. – Albert Tugushevisinstance(x,str)
is correct in Python 3 (str is a basic type). – MasterControlProgram