Python 如何检查变量的类型是否为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4843173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if type of a variable is string?
提问by c_pleaseUpvote
Is there a way to check if the type of a variable in python is a string, like:
有没有办法检查python中变量的类型是否为 a string,例如:
isinstance(x,int);
for integer values?
对于整数值?
回答by Sven Marnach
In Python 2.x, you would do
在 Python 2.x 中,你会做
isinstance(s, basestring)
basestringis the abstract superclassof strand unicode. It can be used to test whether an object is an instance of stror unicode.
basestring是抽象的超类的str和unicode。它可用于测试对象是否是str或的实例unicode。
In Python 3.x, the correct test is
在 Python 3.x 中,正确的测试是
isinstance(s, str)
The bytesclass isn't considered a string type in Python 3.
本bytes类不被认为是在Python 3字符串类型。
回答by dicato
The type module also exists if you are checking more than ints and strings. http://docs.python.org/library/types.html
如果您检查的不仅仅是整数和字符串,类型模块也存在。 http://docs.python.org/library/types.html
回答by Wade Hatler
Edit based on better answer below. Go down about 3 answers and find out about the coolness of basestring.
根据下面更好的答案进行编辑。往下看大约 3 个答案,了解 basestring 的酷炫。
Old answer: Watch out for unicode strings, which you can get from several places, including all COM calls in Windows.
旧答案:注意 unicode 字符串,您可以从多个地方获取它,包括 Windows 中的所有 COM 调用。
if isinstance(target, str) or isinstance(target, unicode):
回答by yprez
Alternative way for Python 2, without using basestring:
Python 2 的替代方法,不使用 basestring:
isinstance(s, (str, unicode))
But still won't work in Python 3 since unicodeisn't defined (in Python 3).
但仍然不能在 Python 3 中工作,因为unicode没有定义(在 Python 3 中)。
回答by Daniil Grankin
Also I want notice that if you want to check whether the type of a variable is a specific kind, you can compare the type of the variable to the type of a known object.
另外我要注意的是,如果您想检查变量的类型是否为特定类型,您可以将变量的类型与已知对象的类型进行比较。
For string you can use this
对于字符串,您可以使用它
type(s) == type('')
回答by André Fratelli
I know this is an old topic, but being the first one shown on google and given that I don't find any of the answers satisfactory, I'll leave this here for future reference:
我知道这是一个古老的话题,但作为第一个在 google 上显示的话题,鉴于我没有找到任何令人满意的答案,我将其留在这里以供将来参考:
sixis a Python 2 and 3 compatibility library which already covers this issue. You can then do something like this:
Six是一个 Python 2 和 3 兼容性库,它已经涵盖了这个问题。然后你可以做这样的事情:
import six
if isinstance(value, six.string_types):
pass # It's a string !!
Inspecting the code, this is what you find:
检查代码,这就是你发现的:
import sys
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str,
else:
string_types = basestring,
回答by mPrinC
If you do not want to depend on external libs, this works both for Python 2.7+ and Python 3 (http://ideone.com/uB4Kdc):
如果您不想依赖外部库,这适用于 Python 2.7+ 和 Python 3 ( http://ideone.com/uB4Kdc):
# your code goes here
s = ["test"];
#s = "test";
isString = False;
if(isinstance(s, str)):
isString = True;
try:
if(isinstance(s, basestring)):
isString = True;
except NameError:
pass;
if(isString):
print("String");
else:
print("Not String");
回答by User
This is how I do it:
这就是我的做法:
if type(x) == type(str()):
回答by fast tooth
I've seen:
我见过:
hasattr(s, 'endswith')
回答by Texom512
In Python 3.x or Python 2.7.6
在 Python 3.x 或 Python 2.7.6 中
if type(x) == str:

