Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

python - 如何检查变量的类型是否为字符串?(How to check if type of a variable is string?)

Is there a way to check if the type of a variable in python is string.

(有没有办法检查python中的变量类型是否为字符串。)

like:

(喜欢:)

isinstance(x,int);

for integer values?

(对于整数值?)

  ask by c_pleaseUpvote translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Python 2.x, you would do

(在Python 2.x中,你会这样做)

isinstance(s, basestring)

basestring is the abstract superclass of str and unicode .

(basestringstrunicode抽象超类 。)

It can be used to test whether an object is an instance of str or unicode .

(它可用于测试对象是str还是unicode的实例。)


In Python 3.x, the correct test is

(在Python 3.x中,正确的测试是)

isinstance(s, str)

The bytes class isn't considered a string type in Python 3.

(在Python 3中, bytes类不被视为字符串类型。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...