How to check if variable is string with python 2 and 3 compatibility

后端 未结 10 2352
有刺的猬
有刺的猬 2020-12-04 08:47

I\'m aware that I can use: isinstance(x, str) in python-3.x but I need to check if something is a string in python-2.x as well. Will isinstance(x, str)

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 09:32

    If you're writing 2.x-and-3.x-compatible code, you'll probably want to use six:

    from six import string_types
    isinstance(s, string_types)
    

提交回复
热议问题