Given a string and we have to check whether it contains only digits or not in Python.

给定一个字符串,我们必须检查它在Python中是否仅包含数字。

To check that a string contains only digits (or a string has a number) – we can use isdigit() function, it returns true, if all characters of the string are digits.

检查字符串是否仅包含数字 (或字符串包含数字 ),可以使用isdigit()函数 ,如果字符串的所有字符均为数字,则返回true 。

Syntax:

句法:

    string.isdigit()

Example:

例:

    Input:
str1 = "8789"
str2 = "Hello123"
str3 = "123Hello"
str4 = "123 456" #contains space
# function call
str1.isdigit()
str2.isdigit()
str3.isdigit()
str4.isdigit()
Output:
True
False
False
False

Python code to check whether a strings contains a number or not

检查字符串是否包含数字的Python代码

# python program to check whether a string
# contains only digits or not
# variables declaration & initializations
str1 = "8789"
str2 = "Hello123"
str3 = "123Hello"
str4 = "123 456"    #contains space
# checking
print("str1.isdigit(): ", str1.isdigit())
print("str2.isdigit(): ", str2.isdigit())
print("str3.isdigit(): ", str3.isdigit())
print("str4.isdigit(): ", str4.isdigit())
# checking & printing messages
if str1.isdigit():
print("str1 contains a number")
else:
print("str1 does not contain a number")
if str2.isdigit():
print("str2 contains a number")
else:
print("str2 does not contain a number")
if str3.isdigit():
print("str3 contains a number")
else:
print("str3 does not contain a number")
if str4.isdigit():
print("str4 contains a number")
else:
print("str4 does not contain a number")

Output

输出量

str1.isdigit():  True
str2.isdigit():  False
str3.isdigit():  False
str4.isdigit():  False
str1 contains a number
str2 does not contain a number
str3 does not contain a number
str4 does not contain a number

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

检查字符串是否包含数字的Python程序相关推荐

  1. 检查字符串是否包含数字

    创建JavaBean类 public class StringUtil3 {private String str; //要检查的字符串private boolean hasNum; //是否包含数字p ...

  2. Python检查字符串是否包含另一个字符串

    String manipulation is a common task in any programming language. Python provides two common ways to ...

  3. 如何在Java中检查字符串是否为数字

    在解析字符串之前,如何检查字符串是否为数字? #1楼 解析它(即使用Integer#parseInt )并简单地捕获异常. =) 需要澄清的是:parseInt函数检查它是否可以在任何情况下(显然)都 ...

  4. python中isdigit函数什么意思_python字符串是否为数字类型-python isdigit函数-isdigit函数 python-python isdigit函数使用-嗨客网...

    Python字符串是否是数字教程 在开发过程中,有时候我们需要判断一个 Python isdigit()函数详解 语法 str.isdigit() -> bool 参数 参数 描述 str 表示 ...

  5. 如何检查字符串是否包含特定单词?

    考虑: $a = 'How are you?';if ($a contains 'are')echo 'true'; 假设我有上面的代码, if ($a contains 'are') ,写语句的正确 ...

  6. 如何检查字符串是否包含特定的单词? [英]How do I check if a string contains a specific word?

    Consider: 考虑: $a = 'How are you?';if ($a contains 'are')echo 'true'; Suppose I have the code above, ...

  7. 输入字符串,包含数字,大小写字母,编程输出出现做多的数字的和

    题目描述: 输入字符串,包含数字,大小写字母,编程输出出现做多的数字的和. 思路: 1.创建输入对象 2.输入字符串 3.利用正则将字母分离出,剩余的每一个字符串即为待统计的每一个数字,存入字符串数组 ...

  8. 如何检查字符串是否包含Ruby中的子字符串?

    我有一个字符串变量,内容如下: varMessage = "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n""/my/name/is/balaji.s ...

  9. java中如何检查字符串都是数字_如何在Java中检查字符串是否为数字?

    我们将检查字符串是否为数字-借助逻辑,我们将解决此问题,第一步,我们将使用一个名为str的字符串变量,并将任何值存储在其中. 在第二步中,我们将使用一个名为str_numeric的布尔变量,该变量存储 ...

最新文章

  1. Spring MVC静态资源处理
  2. 前端经典案例——购物车,Jquery实现
  3. Python 列表 count( )方法
  4. 小波变换和motion信号处理(三)
  5. 原生js发送ajax请求
  6. 疯狂讲义java_《疯狂Java讲义》 1-概述
  7. 固体火箭发动机装药拓扑优化(观点)
  8. EventBus源码解析 1
  9. 光纤跳线接口_不同速率光模块与光纤跳线的连接使用方案
  10. 从0开始的appium+Android+python自动抢红包世界生活
  11. javaweb验证码明明输入正确却还是提示错误,验证码session不同步、不一致问题
  12. np.linalg.inv方法详解
  13. Microsoft Teams管理(一)
  14. 配置路由urlconf
  15. 《小岛经济学》八、金本位的破灭、房地产的泡沫
  16. java版-五子棋-个人-源码参考
  17. [转] Android开发环境的搭建 Android虚拟机搭建 安卓开发环境搭建
  18. mysql 计算gps坐标距离_mysql 下 计算 两点 经纬度 之间的距离(转)
  19. Keras Conv1d 参数及输入输出详解
  20. idea+git合并分支解决冲突及详解

热门文章

  1. c语言2048项目报告,c语言----项目_小游戏2048
  2. linux终端炫酷命令,你不得不知道11个炫酷的 Linux 终端命令
  3. docker mysql57_docker安装mysql57
  4. repositoryitemlookupedit根据每行的id绑定数据_一种根据数据库自增ID生成唯一ID的解决方案...
  5. python医学图像分割_基于cv2的医学图像分割
  6. 软件工程(Rational统一过程)
  7. 三种平摊分析的方法分别为_干货|电工必须学会的三极管电路分析方法
  8. python 文档字符串_新款Python文档字符串生成器来了
  9. 创造型模式——单例模式
  10. HTTP 视频怎么在 MIP 页面中使用?