1. 程式人生 > >python 判斷一個數為?

python 判斷一個數為?

class OS color bsp body dig ins pos 字符

1. 判斷一個變量是否數字(整數、浮點數)?

1 instance(a, (int, long, float))
2 
3 True
4 
5 
6 isinstance(a, (int, long, float))
7 
8 False

2. 判斷一個字符串的內容是否表示數字(整數、浮點數)?

1 foo = 123.456
2 
3 foo.replace(., ‘‘, 1).isdigit()
4 True
5 
6 
7 bar = 12.34.56
8 bar.replace(., ‘‘, 1).isdigit()
9 False

python 判斷一個數為?