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

How to determine whether it is a string in python

青灯夜游
Release: 2023-01-03 09:26:20
Original
24897 people have browsed it

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) #判断是否是字符串型
Copy after login

Output:

True
Copy after login

python isinstance()

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

Syntax:

isinstance(object, classinfo)
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template