检查字符串是否为整数的最佳方法是什么? [英] Best way to check if string is an integer?
问题描述
这是检查字符串是数字还是字符的最佳方式?
如果我必须检查
,那么第二个例子的时间非常昂贵很多字符串?
这个
value = raw_input()
尝试:
value = int(value)
除了ValueError:
print" value is an integer"
或:
c = raw_input(" yo:")
如果c在''0123456789''中:
print" integer"
else:
print" char"
或其他一些方式?
which is the best way to check if a string is an number or a char?
could the 2nd example be very expensive timewise if i have to check a
lot of strings?
this
value = raw_input()
try:
value = int(value)
except ValueError:
print "value is not an integer"
or:
c=raw_input("yo: ")
if c in ''0123456789'':
print "integer"
else:
print "char"
or some other way?
推荐答案
sk*******@yahoo.se 写道:
sk*******@yahoo.se wrote:
>这是检查字符串是数字还是字符的最佳方法?
如果我必须检查,第二个例子可能是非常昂贵的时间一个很多字符串?
这个
值= raw_input()
尝试:
value = int(value)
除了ValueError:
print" value is an integer"
或:
c = raw_input(" yo:")
如果在''0123456789'中的c:
print" integer"
else:
打印" char"
或其他一些方式?
>which is the best way to check if a string is an number or a char?
could the 2nd example be very expensive timewise if i have to check a
lot of strings?
this
value = raw_input()
try:
value = int(value)
except ValueError:
print "value is not an integer"
or:
c=raw_input("yo: ")
if c in ''0123456789'':
print "integer"
else:
print "char"
or some other way?
我总是第一次这样做办法。它更简单,而且应该更快。另外,
第二种方式只适用于单位数字(你必须使用for循环迭代整个字符串,以便在数字上使用它<
br />
多于一位数。)
I always do it the first way. It is simpler, and should be faster. Also,
the second way will only work on single-digit numbers (you would have to
iterate over the entire string with a for loop to use it on numbers with
more than one digit).
4月5日,6:19 * pm,skanem ... @ yahoo.se写道:
On Apr 5, 6:19*pm, skanem...@yahoo.se wrote:
这是检查字符串是数字还是字符的最佳方式?
第二个例子可能是非常昂贵的时间,如果我有检查一下
很多字符串?
which is the best way to check if a string is an number or a char?
could the 2nd example be very expensive timewise if i have to check a
lot of strings?
您可能对str.isdigit感兴趣:
You might be interested in str.isdigit:
>> print str.isdigit .__ doc__
>>print str.isdigit.__doc__
S.isdigit() - bool
如果S中的所有字符都是数字,则返回True
且S中至少有一个字符,否则返回False
Mark
S.isdigit() -bool
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
Mark
4月6日上午9:25,Mark Dickinson< dicki ... @ gmail.comwrote:
On Apr 6, 9:25 am, Mark Dickinson <dicki...@gmail.comwrote:
4月5日下午6:19,skanem ... @ yahoo.se写道:
On Apr 5, 6:19 pm, skanem...@yahoo.se wrote:
这是检查一个是否是最好的方法string是一个数字还是一个char?
如果我必须检查一下
很多字符串,第二个例子在时间上会非常昂贵吗?
which is the best way to check if a string is an number or a char?
could the 2nd example be very expensive timewise if i have to check a
lot of strings?
您可能对str.isdigit感兴趣:
You might be interested in str.isdigit:
> ; print str.isdigit .__ doc__
>print str.isdigit.__doc__
S.isdigit()-bool
如果全部返回True S中的字符是数字
并且S中至少有一个字符,否则为False。
S.isdigit() -bool
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
这不适合负面整数。
This doesn''t cater for negative integers.
这篇关于检查字符串是否为整数的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!