Home>Article>Backend Development> How to determine whether it is a string in python

How to determine whether it is a string in python

青灯夜游
青灯夜游 Original
2021-03-01 14:50:12 24805browse

In python, you can use the isinstance() function to determine whether it is a string. The syntax format is "isinstance(object, basestring)"; the isinstance() function is used to determine whether an object is a known type. .

How to determine whether it is a string in python

The operating environment of this tutorial: windows7 system, python3 version, DELL G3 computer

python determines whether it is a string

>>> s = 'abc' >>> isinstance(s,basestring) #判断是否是字符串型

Output:

True

python isinstance()

isinstance() function to determine whether an object is an Known type, similar to type().

Syntax:

isinstance(object, classinfo)

Parameters:

object -- Instance object. classinfo -- can be a direct or indirect class name, a primitive type, or a tuple of them.

Return value:

If the type of the object is the same as the type of parameter two (classinfo), it returns True, otherwise it returns False. Related recommendations:Python3 video tutorial

Example:

>>> isinstance(1, int)  #判断是否是int型 True >>> isinstance(1.0, float) #判断是否是float型 True >>> s = 'hello' >>> isinstance(s,basestring) #判断是否是字符串型 True >>>isinstance(s,dict) #判断对象a是否为字典,如果为真,会打印True,如为假,打印False。 False

For more programming-related knowledge, please visit:Programming Video! !

The above is the detailed content of How to determine whether it is a string in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to lower python version Next article:How to lower python version