现在位置: 首页 > Python 基础教程 > 正文

Python isdigit()方法

Python 字符串 Python 字符串


描述

Python isdigit() 方法检测字符串是否只由数字组成,只对 0 和 正数有效。

语法

isdigit()方法语法:

str.isdigit()

参数

  • 无。

返回值

如果字符串只包含数字则返回 True 否则返回 False。

实例

以下实例展示了isdigit()方法的实例:

实例

#!/usr/bin/python # -*- coding: UTF-8 -*- str = "123456" # 字符串只包含数字 print (str.isdigit()) str = "this is string example....wow!!!" print (str.isdigit()) str = "0" print (str.isdigit()) str = "-1" print (str.isdigit())

以上实例输出结果如下:

True
False
True
False

Python 字符串 Python 字符串