String length without len function

前端 未结 17 1315
逝去的感伤
逝去的感伤 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条回答
  •  猫巷女王i
    2020-12-10 21:51

    a = 'malayalam'
    length = 0
    for i in a:
        if i == "":
            break
        else:
            length+=1
    
    print length
    

    This code verifies the length of a string by counting until ""(end of the string ).If the string reaches an end, the loop breaks and will return the final length of the string.

提交回复
热议问题