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

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

    Its simple, use the following code (we assume the object mentioned to be obj)-

    if type(obj) == str:
        print('It is a string')
    else:
        print('It is not a string.')
    

提交回复
热议问题