日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

检查字符串是否包含数字的Python程序

發布時間:2025/3/11 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 检查字符串是否包含数字的Python程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 callstr1.isdigit()str2.isdigit()str3.isdigit()str4.isdigit()Output:TrueFalseFalseFalse

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程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。