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

前端 未结 14 1995
天涯浪人
天涯浪人 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:38

    I found this ans more pythonic:

    if type(aObject) is str:
        #do your stuff here
        pass
    

    since type objects are singleton, is can be used to do the compare the object to the str type

提交回复
热议问题