python 字符串 变量

Sometimes we want to check if the variable or input argument is String and then only perform further actions. We can use isinstance() function to verify that a variable is string or not.

有时我们想检查变量或输入参数是否为String,然后仅执行进一步的操作。 我们可以使用isinstance()函数来验证变量是否为字符串。

Python变量是字符串 (Python Variable is String)

Let’s look at a simple example to check if a variable is a string or not.

让我们看一个简单的示例,检查变量是否为字符串。

i = 5  # not strprint(isinstance(i, str))s = 'abc'  # stringprint(isinstance(s, str))

Output:

输出:

False
True

Python函数输入为字符串 (Python Function Input is String)

If you look at above example, we are creating the variable so we already know its type. However, if we have to define a function to process input string then it’s a good idea to check if the input supplied is a string or not.

如果您看上面的示例,我们正在创建变量,因此我们已经知道它的类型。 但是,如果我们必须定义一个函数来处理输入字符串,那么最好检查提供的输入是否为字符串。

Let’s say we have a function defined as:

假设我们有一个定义为的函数:

def process_string(input_str):print('Processing', input_str)

If we have following code snippet to execute this function:

如果我们有以下代码片段执行此功能:

process_string('abc')process_string(100)

The output will be:

输出将是:

Processing abc
Processing 100

Since we don’t have validation in place for the input argument, our function is processing non-string arguments too.

由于我们没有针对输入参数的验证,因此我们的函数也正在处理非字符串参数。

If we want our function to run its logic for string argument only, then we can add a validation check using isinstance() function.

如果我们希望函数仅对字符串参数运行其逻辑,则可以使用isinstance()函数添加验证检查。

def process_string(input_str):if (isinstance(input_str, str)):print('Processing', input_str)else:print('Input Must be String')

Now when we call this function as:

现在,当我们将此函数称为:

process_string('abc')process_string(100)

The output will be:

输出将是:

Processing abc
Input Must be String

We can use isinstance() function to check the type of any variable or function arguments.

我们可以使用isinstance()函数来检查任何变量或函数参数的类型。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: isinstance() api doc

参考: isinstance()API文档

翻译自: https://www.journaldev.com/23507/python-check-variable-is-string

python 字符串 变量

python 字符串 变量_Python检查变量是字符串相关推荐

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

    python 字符串 变量 Python | 检查变量是否为字符串 (Python | Check if a variable is a string) To check whether a defi ...

  2. python静态变量_python静态变量

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 我这里的代码实现了静态变量的装饰器. 但是,我发现如果我多次运行这个函数,每次调 ...

  3. python字符串分片_Python第六章字符串ppt

    PPT内容 这是Python第六章字符串ppt,包括了概述,通用序列操作,字符串格式化,字符串方法,正则表达式简介等内容,欢迎点击下载. Python语言编程导论 第六章  字符串 内容提要 概述 通 ...

  4. python函数里面引用外部变量_Python基础 变量进阶

    目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引用 传递的 1.1 ...

  5. java python 运行 内存_Python C Java中字符串在内存中的存储

    Python 昨天在讲Python的过程中,发现了一个小问题,当时课上没有解决,下面特意查阅资料解决了一下. a = "123" b = "123" print ...

  6. python定义变量_Python基础 变量的基本使用

    变量的基本使用 程序就是用来处理数据的,而变量就是用来存储数据的 目标 变量定义 变量的类型 变量的命名 01. 变量定义 在 Python 中,每个变量 在使用前都必须赋值,变量 赋值以后 该变量 ...

  7. python创建变量_python创建变量

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 创建: 当python变量'赋值'时,根据值类型创建变量,如:a=1引用: 当参 ...

  8. 怎么设置python解释器path变量_python环境变量设置

    在默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. python环境变量设置 1. 首先需要在系统中注册python环境 ...

  9. python中abc属于字符串吗_Python基础学习:字符串

    Python 版本: 3.6.2 操作系统: Windows 作者: SmallWZQ 在 Python 中,字符串也是一种数据类型.相比其它数据类型,字符串算是比较复杂的.为 何呢?因为字符串不仅包 ...

最新文章

  1. Dockerfile文件全面详解
  2. JAVA_HOME PATH CLASSPATH
  3. H5前期知识点总结 9月12日
  4. Mybatis 插入时获取主键的方式
  5. threading注意点(python 版)
  6. 设计模式GOF23大纲
  7. 进击的华为云医疗AI:顶会两篇论文连发,研究和落地加速
  8. 蓝牙条码扫描枪如何选购
  9. 矩阵论与计算机英语论文,矩阵论翻译论文.pdf
  10. 串口调试工具 minicom使用教程
  11. MCSA/MCSE Windows Server 2016认证的学习目录
  12. excel如何快速将英文表格翻译为中文表格
  13. 思科网络综合实验(服务器,三层交换机,路由器)
  14. Win7下安装Ubuntu(双硬盘)的简要步骤
  15. 今日接手一个项目,服务器重启后,mysql启动失败
  16. 版本 87.0.4280.66_iOS 14.2 Beta 2发布,修复闪退与优化改进
  17. 第三届全国大学生算法设计与编程挑战赛 (冬季赛)部分题解
  18. ms office excel2013教程 - randbetween函数与选择性粘贴
  19. Open Images Dataset V6 简介
  20. 大数据24小时:孙彬出任乐视云新CEO,趣店数百万学生信息数据疑似泄露

热门文章

  1. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
  2. 给员工授予svn相关权限
  3. [转载] Python内置函数-min函数和max函数-详解
  4. django models 配置
  5. 何登成 MYSQL 博客
  6. Balanced Numbers数位dp
  7. ExtJS视频学习笔记
  8. 【原创】使用Ultra Librarian为Altium Designer 09生成元器件库
  9. 中国移动创新系列丛书《OPhone应用开发权威指南》读者交流活动圆满结束
  10. 多分类支持向量机及其Python实现