python 字符串 变量_检查变量是否为字符串的Python程序

python 字符串 变量

Python | 检查变量是否为字符串 (Python | Check if a variable is a string)

To check whether a defined variable is a string type or not, we can use two functions which are Python library functions,

要检查定义的变量是否为字符串类型,我们可以使用两个函数,它们是Python库函数,

  1. Using isinstance()

    使用isinstance()

  2. Using type()

    使用type()

1)使用isinstance()函数检查变量是否为字符串 (1) Checking a variable is a string or not using isinstance() function)

isinstance() function accepts two parameters – 1) variable name (object) and 2) data type (class) and returns whether an object is an instance of a class or of a subclass thereof.

isinstance()函数接受两个参数– 1)变量名( 对象 )和2)数据类型( 类 ),并返回对象是类的实例还是其子类的实例。

Syntax:

句法:

    isinstance(obj, class_or_tuple)

Example:

例:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # a string variable 
# checking types
if isinstance(a, str):
print("Variable \'a\' is a type of string.")
else:
print("Variable \'a\' is not a type of string.")
if isinstance(b, str):
print("Variable \'b\' is a type of string.")
else:
print("Variable \'b\' is not a type of string.")
if isinstance(c, str):
print("Variable \'c\' is a type of string.")
else:
print("Variable \'c\' is not a type of string.")
if isinstance(d, str):
print("Variable \'d\' is a type of string.")
else:
print("Variable \'d\' is not a type of string.")
if isinstance(e, str):
print("Variable \'e\' is a type of string.")
else:
print("Variable \'e\' is not a type of string.")

Output

输出量

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.

2)使用type()函数检查变量是否为字符串 (2) Checking a variable is string using type() function)

type() function accepts one parameter (others are optional), and returns its type.

type()函数接受一个参数(其他参数是可选的),并返回其类型。

Syntax:

句法:

    type(object)

Example:

例:

# variables
a = 100   # an integer variable
b = 10.23 # a float variable
c = 'A'   # a character variable 
d = 'Hello'   # a string variable
e = "Hello"   # a string variable 
# checking types
if type(a) == str:
print("Variable \'a\' is a type of string.")
else:
print("Variable \'a\' is not a type of string.")
if type(b) == str:
print("Variable \'b\' is a type of string.")
else:
print("Variable \'b\' is not a type of string.")
if type(c) == str:
print("Variable \'c\' is a type of string.")
else:
print("Variable \'c\' is not a type of string.")
if type(d) == str:
print("Variable \'d\' is a type of string.")
else:
print("Variable \'d\' is not a type of string.")
if type(e) == str:
print("Variable \'e\' is a type of string.")
else:
print("Variable \'e\' is not a type of string.")

Output

输出量

Variable 'a' is not a type of string.
Variable 'b' is not a type of string.
Variable 'c' is a type of string.
Variable 'd' is a type of string.
Variable 'e' is a type of string.

翻译自: https://www.includehelp.com/python/check-whether-a-variable-is-a-string-or-not.aspx

python 字符串 变量

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542273.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql shell

mysql 查询10分钟以内的数据:select *from t_agent where int_last_login>CURRENT_TIMESTAMP - INTERVAL 10 MINUTE; mysql关联多表进行update更新操作UPDATE TrackINNER JOIN MVON Track.trkidMV.mvidSET Track.is_showMV.is_showWHERE trkid<6等同于UPDATE Track,MVSET…

kaggle计算机视觉比赛技巧,9. 计算机视觉 - 9.12. 实战Kaggle比赛:图像分类(CIFAR-10) - 《动手学深度学习》 - 书栈网 · BookStack...

9.12. 实战Kaggle比赛&#xff1a;图像分类(CIFAR-10)到目前为止&#xff0c;我们一直在用Gluon的data包直接获取NDArray格式的图像数据集。然而&#xff0c;实际中的图像数据集往往是以图像文件的形式存在的。在本节中&#xff0c;我们将从原始的图像文件开始&#xff0c;一步…

qthread中获取当前优先级_Linux中强大的top命令

top命令算是最直观、好用的查看服务器负载的命令了。它实时动态刷新显示服务器状态信息&#xff0c;且可以通过交互式命令自定义显示内容&#xff0c;非常强大。在终端中输入top&#xff0c;回车后会显示如下内容&#xff1a;top - 21:48:39 up 8:57, 2 users, load average: 0…

JavaScript中带示例的String repeat()方法

JavaScript | 字符串repeat()方法 (JavaScript | String repeat() Method) The String.repeat() method in JavaScript is used to generate a string by repeating the calling string n number of times. n can be any integer from o to any possible number in JavaScript.…

Python生成验证码

#!/usr/bin/env python #coding:utf8 import random #方法1&#xff1a; str_codezxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789new_codefor i in range(4):   new_coderandom.choice(str_code)print new_code #方法2&#xff1a; new_code[]def str_code…

snmp 获得硬件信息_计算机网络基础课程—简单网络管理协议(SNMP)

简单网络管理协议(Simple Network Management Protocol)•除了提供网络层服务的协议和使用那些服务的应用程序&#xff0c;因特网还需要运行一些让管理员进行设备管理、调试问题、控制路由、监测机器状态的软件。这种行为称为网络管理。••随着网络技术的飞速发展&#xff0c;…

僵尸毁灭工程 服务器已停止运行,《僵尸毁灭工程》steam is not enabled错误解决方法...

Steam 上面的 Project Zomboid 因为带有 VAC 所以建服开服需要 Steam服务器认证&#xff0c;这也是出现 steam is not enabled 错误主要原因&#xff0c;也是无法和普通零售正版所建的服务器联机的罪魁祸首。分两种情况(下面 Project Zomboid 均简称PZ)&#xff1a;1、steam版P…

spring boot 1.4默认使用 hibernate validator

spring boot 1.4默认使用 hibernate validator 5.2.4 Final实现校验功能。hibernate validator 5.2.4 Final是JSR 349 Bean Validation 1.1的具体实现。 How to disable Hibernate validation in a Spring Boot project As [M. Deinum] mentioned in a comment on my original …

python mpi开销_GitHub - hustpython/MPIK-Means

并行计算的K-Means聚类算法实现一&#xff0c;实验介绍聚类是拥有相同属性的对象或记录的集合&#xff0c;属于无监督学习&#xff0c;K-Means聚类算法是其中较为简单的聚类算法之一&#xff0c;具有易理解&#xff0c;运算深度块的特点.1.1 实验内容通过本次课程我们将使用C语…

服务器修改开机启动项,启动项设置_服务器开机启动项

最近很多观众老爷在苦觅关于启动项设置的解答&#xff0c;今天钦编为大家综合5条解答来给大家解开疑惑&#xff01; 有98%玩家认为启动项设置_服务器开机启动项值得一读&#xff01;启动项设置1.如何在bios设置硬盘为第一启动项详细步骤根据BIOS分类的不同操作不同&#xff1a;…

字符串查找字符出现次数_查找字符串作为子序列出现的次数

字符串查找字符出现次数Description: 描述&#xff1a; Its a popular interview question based of dynamic programming which has been already featured in Accolite, Amazon. 这是一个流行的基于动态编程的面试问题&#xff0c;已经在亚马逊的Accolite中得到了体现。 Pr…

Ubuntu 忘记密码的处理方法

Ubuntu系统启动时选择recovery mode&#xff0c;也就是恢复模式。接着选择Drop to root shell prompt ,也就是获取root权限。输入命令查看用户名 cat /etc/shadow &#xff0c;$号前面的是用户名输入命令&#xff1a;passwd "用户名" 回车就可以输入新密码了转载于:…

服务器mdl文件转换,Simulink Project 中 MDL 到 SLX 模型文件格式的转换

打开弹体示例项目并将 MDL 文件另存为 SLX运行以下命令以创建并打开“sldemo_slproject_airframe”示例的工作副本。Simulink.ModelManagement.Project.projectDemo(airframe, svn);rebuild_s_functions(no_progress_dialog);Creating sandbox for project.Created example fil…

vue 修改div宽度_Vue 组件通信方式及其应用场景总结(1.5W字)

前言相信实际项目中用过vue的同学&#xff0c;一定对vue中父子组件之间的通信并不陌生&#xff0c;vue中采用良好的数据通讯方式&#xff0c;避免组件通信带来的困扰。今天笔者和大家一起分享vue父子组件之间的通信方式&#xff0c;优缺点&#xff0c;及其实际工作中的应用场景…

Java System类identityHashCode()方法及示例

系统类identityHashCode()方法 (System class identityHashCode() method) identityHashCode() method is available in java.lang package. identityHashCode()方法在java.lang包中可用。 identityHashCode() method is used to return the hashcode of the given object – B…

Linux中SysRq的使用(魔术键)

转&#xff1a;http://www.chinaunix.net/old_jh/4/902287.html 魔术键&#xff1a;Linux Magic System Request Key Hacks 当Linux 系统不能正常响应用户请求时, 可以使用SysRq小工具控制Linux. 一 SysRq的启用与关闭 要想启用SysRq, 需要在配置内核时设置Magic SysRq key (CO…

链接服务器访问接口返回了消息没有活动事务,因为链接服务器 SQLEHR 的 OLE DB 访问接口 SQLNCLI10 无法启动分布式事务。...

查看一下MSDTC啟動是否正確1、运行 regedt32&#xff0c;浏览至 HKEY_LOCAL_MACHINE\Software\Microsoft\MSDTC。添加一个 DWORD 值 TurnOffRpcSecurity&#xff0c;值数据为 1。2、重启MS DTC服务。3、打开“管理工具”的“组件服务”。a. 浏览至"启动管理工具"。b.…

micropython 蜂鸣器_基于MicroPython的TPYBoard微信远程可燃气体报警器的设计与实现...

前言在我们平时的生活中&#xff0c;经常看到因气体泄漏发生爆炸事故的新闻。房屋起火、人体中毒等此类的新闻报道层出不穷。这种情况下&#xff0c;人民就发明了可燃气体报警器。当工业环境、日常生活环境(如使用天然气的厨房)中可燃性气体发生泄露&#xff0c;可燃气体报警器…

Java PropertyPermission getActions()方法与示例

PropertyPermission类的getActions()方法 (PropertyPermission Class getActions() method) getActions() method is available in java.util package. getActions()方法在java.util包中可用。 getActions() method is used to get the list of current actions in the form of…

源码安装nginx以及平滑升级

源码安装nginx以及平滑升级作者&#xff1a;尹正杰版权声明&#xff1a;原创作品&#xff0c;谢绝转载&#xff01;否则将追究法律责任。欢迎加入&#xff1a;高级运维工程师之路 598432640这个博客不方便上传软件包&#xff0c;我给大家把软件包放到百度云链接&#xff1a;htt…