How to find out if a Python object is a string?

前端 未结 14 1972
天涯浪人
天涯浪人 2020-11-30 17:47

How can I check if a Python object is a string (either regular or Unicode)?

14条回答
  •  死守一世寂寞
    2020-11-30 18:34

    Python 3

    In Python 3.x basestring is not available anymore, as str is the sole string type (with the semantics of Python 2.x's unicode).

    So the check in Python 3.x is just:

    isinstance(obj_to_test, str)
    

    This follows the fix of the official 2to3 conversion tool: converting basestring to str.

提交回复
热议问题