python 判断一个数为?

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

 

posted @ 2018-02-25 11:42  shunyu  阅读(746)  评论(0)    收藏  举报