博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
检查字符串是否包含数字的Python程序
阅读量:2530 次
发布时间:2019-05-11

本文共 1738 字,大约阅读时间需要 5 分钟。

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 & initializationsstr1 = "8789"str2 = "Hello123"str3 = "123Hello"str4 = "123 456"    #contains space # checkingprint("str1.isdigit(): ", str1.isdigit())print("str2.isdigit(): ", str2.isdigit())print("str3.isdigit(): ", str3.isdigit())print("str4.isdigit(): ", str4.isdigit())# checking & printing messagesif 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():  Truestr2.isdigit():  Falsestr3.isdigit():  Falsestr4.isdigit():  Falsestr1 contains a numberstr2 does not contain a numberstr3 does not contain a numberstr4 does not contain a number

翻译自:

转载地址:http://byxzd.baihongyu.com/

你可能感兴趣的文章
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
P2731 骑马修栅栏 欧拉函数
查看>>
sort函数
查看>>
CentOS-6.3安装配置Nginx
查看>>
女陔说"你不懂我", 到底什么意思
查看>>
uva11149
查看>>
Windows下安装Redis
查看>>
20155339 《信息安全系统设计基础》课程总结
查看>>
javascript 正则表达式学习
查看>>
ASCII代码 简介
查看>>
SSL协议之数据加密过程详解
查看>>
详解C# 匿名对象(匿名类型)、var、动态类型 dynamic
查看>>
centos7 开放端口
查看>>
迷宫实现
查看>>
如何使用Transact-SQL进行事务处理[示例]
查看>>