Counting number of digits of input using python

前端 未结 5 766
离开以前
离开以前 2021-01-19 18:46

I am trying to count the number of digits of an input. However, whenever I input 10 or 11 or any two digit number, the output is 325.

5条回答
  •  后悔当初
    2021-01-19 19:09

    I would not convert that beautiful input to int to be honest.

    print(len(input())
    

    would be sufficient.

    An easily understandable one liner that no one can complain about.

    • But of course, if negative sign bothers you like wisty said,

      len(str(abs(int (v))))
      

      will be safer for sure.

    • Again, if you are worried about the non numeric inputs like mulliganaceous said, you better cover that case.

      str = input()
      if str.isnumeric():
          print(len(str(abs(v))))
      else:
          print("bad input")
      

提交回复
热议问题