Skip to main content
Remove pointless (bad) extra option and correct grammar
Source Link
LeopardShark
  • 4.5k
  • 4
  • 21
  • 37

Use isinstance to check if o is an instance of str or any subclass of str:

if isinstance(o, str):

To check if the type of o is exactly str, excluding subclasses of str:

if type(o) is str:

Another alternative to the above:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.


Checking for strings in Python 2

For Python 2, this is a better way to check if o is a string:

if isinstance(o, basestring):

because this will also catch Unicode strings. unicode is not a subclass of str; whereas, both str and unicode are subclasses of basestring. In Python 3, basestring no longer exists since there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):

Use isinstance to check if o is an instance of str or any subclass of str:

if isinstance(o, str):

To check if the type of o is exactly str, excluding subclasses of str:

if type(o) is str:

Another alternative to the above:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.


Checking for strings in Python 2

For Python 2, this is a better way to check if o is a string:

if isinstance(o, basestring):

because this will also catch Unicode strings. unicode is not a subclass of str; whereas, both str and unicode are subclasses of basestring. In Python 3, basestring no longer exists since there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):

Use isinstance to check if o is an instance of str or any subclass of str:

if isinstance(o, str):

To check if the type of o is exactly str, excluding subclasses of str:

if type(o) is str:

See Built-in Functions in the Python Library Reference for relevant information.


Checking for strings in Python 2

For Python 2, this is a better way to check if o is a string:

if isinstance(o, basestring):

because this will also catch Unicode strings. unicode is not a subclass of str; both str and unicode are subclasses of basestring. In Python 3, basestring no longer exists since there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):
Formatting, grammar style, condense text.
Source Link
Mateen Ulhaq
  • 27.8k
  • 21
  • 121
  • 155

ToUse isinstance to check if o is an instance of str or any subclass of str, use isinstance (this would be the "canonical" way):

if isinstance(o, str):

To check if the type of o is exactly str, (exclude subclasses)excluding subclasses of str:

if type(o) is str:

The following also works, and can be useful in some casesAnother alternative to the above:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.

 

Checking for strings in Python 2

One more note: in this case, if you're usingFor Python 2, you may actually wantthis is a better way to usecheck if o is a string:

if isinstance(o, basestring):

because this will also catch Unicode strings. (unicode is not a subclass of str; whereas, both str and unicode are subclasses of basestring). Note thatIn Python 3, basestring no longer exists in Python 3, wheresince there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):

To check if o is an instance of str or any subclass of str, use isinstance (this would be the "canonical" way):

if isinstance(o, str):

To check if the type of o is exactly str (exclude subclasses):

if type(o) is str:

The following also works, and can be useful in some cases:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.

One more note: in this case, if you're using Python 2, you may actually want to use:

if isinstance(o, basestring):

because this will also catch Unicode strings (unicode is not a subclass of str; both str and unicode are subclasses of basestring). Note that basestring no longer exists in Python 3, where there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):

Use isinstance to check if o is an instance of str or any subclass of str:

if isinstance(o, str):

To check if the type of o is exactly str, excluding subclasses of str:

if type(o) is str:

Another alternative to the above:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.

 

Checking for strings in Python 2

For Python 2, this is a better way to check if o is a string:

if isinstance(o, basestring):

because this will also catch Unicode strings. unicode is not a subclass of str; whereas, both str and unicode are subclasses of basestring. In Python 3, basestring no longer exists since there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):
deleted 2 characters in body
Source Link
user3064538
user3064538

To check if o is an instance of str or any subclass of str, use isinstanceisinstance (this would be the "canonical" way):

if isinstance(o, str):

To check if the type of o is exactly str (exclude subclasses):

if type(o) is str:

The following also works, and can be useful in some cases:

if issubclass(type(o), str):

See Built-in FunctionsBuilt-in Functions in the Python Library Reference for relevant information.

One more note: in this case, if you're using pythonPython 2, you may actually want to use:

if isinstance(o, basestring):

because this will also catch Unicode strings (unicode is not a subclass of str; both str and unicode are subclasses of basestring). Note that basestring no longer exists in pythonPython 3, where there's a strict separationa strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return TrueTrue if xo is an instance of any subclass of any of (str, unicode)(str, unicode):

if isinstance(o, (str, unicode)):

To check if o is an instance of str or any subclass of str, use isinstance (this would be the "canonical" way):

if isinstance(o, str):

To check if the type of o is exactly str (exclude subclasses):

if type(o) is str:

The following also works, and can be useful in some cases:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.

One more note: in this case, if you're using python 2, you may actually want to use:

if isinstance(o, basestring):

because this will also catch Unicode strings (unicode is not a subclass of str; both str and unicode are subclasses of basestring). Note that basestring no longer exists in python 3, where there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if x is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):

To check if o is an instance of str or any subclass of str, use isinstance (this would be the "canonical" way):

if isinstance(o, str):

To check if the type of o is exactly str (exclude subclasses):

if type(o) is str:

The following also works, and can be useful in some cases:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.

One more note: in this case, if you're using Python 2, you may actually want to use:

if isinstance(o, basestring):

because this will also catch Unicode strings (unicode is not a subclass of str; both str and unicode are subclasses of basestring). Note that basestring no longer exists in Python 3, where there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):
added 21 characters in body
Source Link
smci
  • 34.1k
  • 21
  • 117
  • 152
Loading
Removed incorrect option.
Source Link
user2357112
  • 285.6k
  • 32
  • 488
  • 569
Loading
added clarification about basestring not existing in python 3 and some links to the docs
Source Link
Aran-Fey
  • 43.9k
  • 13
  • 113
  • 161
Loading
Put "cannonoical" method first, add `if` syntax to code blocks
Source Link
Stevoisiak
  • 27.7k
  • 32
  • 139
  • 245
Loading
edited to remove html tags and use proper markdown for them, edited the link which was linking to an ancient version of python
Source Link
Loading
added 188 characters in body
Source Link
Fredrik Johansson
  • 27.6k
  • 3
  • 29
  • 17
Loading
Source Link
Fredrik Johansson
  • 27.6k
  • 3
  • 29
  • 17
Loading