---
title: "巧用python判断字符串是否仅包含数字"
url: "https://lerry.me/post/2011/12/check-if-number-with-python"
date: 2011-12-08T15:20:00.000Z
updated: 2026-08-31T10:43:51.967Z
tags:
  - "Python"
language: zh-CN
---

# 巧用python判断字符串是否仅包含数字

今天写程序需要一个功能，判断一个字符串里是否仅包含数字，如'abc521' '123456'这些字符串哪些是紧包含数字，发现了一个很好的方法：

```python
def isNumber(i):
    '''
    check if a string is number
    '''
    try:
        temp = int(i)
        return True
    except:
        return False
```
