String length without len function

前端 未结 17 1313
逝去的感伤
逝去的感伤 2020-12-10 21:29

Can anyone tell me how can I get the length of a string without using the len() function or any string methods. Please anyone tell me as I\'m tapping my head ma

17条回答
  •  生来不讨喜
    2020-12-10 22:04

    You can get the length of the string with a for loop.

    For example, instead of:

    string=input("Enter a string")
    print(len(string))
    

    Do this:

    string=input("Enter a string")
    a=0
    for letter in string:
      a=a+1
    print(a)
    

提交回复
热议问题