如何使用Python 2和3兼容性检查变量是否为字符串
如何使用Python 2和3兼容性检查变量是否为字符串
这个问题已经有答案了:
我知道在 python-3.x 中可以使用:isinstance(x, str)
,但我需要在 python-2.x 中检查某个内容是否为字符串。在 python-2.x 中,isinstance(x, str)
是否像期望的那样工作?还是我需要检查版本并使用isinstance(x, basestr)
?
具体来说,在 python-2.x 中:
>>>isinstance(u"test", str) False
而在 python-3.x 中没有 u\"foo\"
admin 更改状态以发布 2023年5月22日