How can I get the first two digits of a number?

前端 未结 4 1525
-上瘾入骨i
-上瘾入骨i 2020-12-30 19:31

I want to check the first two digits of a number in Python. Something like this:

for i in range(1000):

    if(first two digits of i == 15):
        print(\"         


        
4条回答
  •  星月不相逢
    2020-12-30 20:25

    You can convert your number to string and use list slicing like this:

    int(str(number)[:2])
    

    Output:

    >>> number = 1520
    >>> int(str(number)[:2])
    15
    

提交回复
热议问题