166
a='aaaa'
print isinstance(a, basestring)#true
print isinstance(a, str)#true

4 Answers 4

401

In Python versions prior to 3.0 there are two kinds of strings "plain strings" and "unicode strings". Plain strings (str) cannot represent characters outside of the Latin alphabet (ignoring details of code pages for simplicity). Unicode strings (unicode) can represent characters from any alphabet including some fictional ones like Klingon.

So why have two kinds of strings, would it not be better to just have Unicode since that would cover all the cases? Well it is better to have only Unicode but Python was created before Unicode was the preferred method for representing strings. It takes time to transition the string type in a language with many users, in Python 3.0 it is finally the case that all strings are Unicode.

The inheritance hierarchy of Python strings pre-3.0 is:

          object
             |
             |
         basestring
            / \
           /   \
         str  unicode

'basestring' introduced in Python 2.3 can be thought of as a step in the direction of string unification as it can be used to check whether an object is an instance of str or unicode

>>> string1 = "I am a plain string"
>>> string2 = u"I am a unicode string"
>>> isinstance(string1, str)
True
>>> isinstance(string2, str)
False
>>> isinstance(string1, unicode)
False
>>> isinstance(string2, unicode)
True
>>> isinstance(string1, basestring)
True
>>> isinstance(string2, basestring)
True
3
  • 13
    Did this change in Python 3? Are the new str and byte still children of basestring? Would be worth adding a note about this.
    – MestreLion
    Sep 28, 2012 at 20:27
  • 15
    @MestreLion: It changed; Py3 has no basestring and str and bytes both subclass object directly. But note that this makes sense, since Py2 str is not the same as Py3 bytes. basestring should be thought of as "character string", of which Py3 only has the str. Hence the 2to3 tool replaces basestring with str. Mar 14, 2014 at 16:25
  • In python3 the isinstance(string2, str) will be True all strings are stored as unicoded May 24, 2021 at 14:16
8

All strings are basestrings, but unicode strings are not of type str. Try this instead:

>>> a=u'aaaa'
>>> print isinstance(a, basestring)
True
>>> print isinstance(a, str)
False
4

Really what you're asking is the difference between the basestring and str class.

Str is a class that inherits from basestr. But unicode strings also exist, as could other ones, if you wanted to make one.

>>> a = u'aaaa'
>>> isinstance(a, str)
False
>>> isinstance(a, basestring)
True
1

Basestring is the superclass of string. In your example, a is of type "str" thus, it is both a basestring, and a str

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.