String length without len function

前端 未结 17 1174
逝去的感伤
逝去的感伤 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:06

    easy:

    length=0
    for x in "This is a string":
        length+=1
    print(length)
    
    0 讨论(0)
  • 2020-12-10 22:06

    String length without using len function in python-

    def strlen(myStr):
        if(myStr==""):
           return 0
        else:
           return 1+strlen(myStr[1:])
    
    0 讨论(0)
  • 2020-12-10 22:06

    Create a for loop

    styx = "How do I count this without using Lens function" number = 0 for c in styx: number = 0 + 1 print(number)

    0 讨论(0)
  • 2020-12-10 22:08

    Not very efficient but very concise:

    def string_length(s):    
        if s == '': return 0
        return 1 + string_length(s[1:])
    
    0 讨论(0)
  • 2020-12-10 22:09
    def length(object):#Define the length calculation function
    
        count = 0  #initializing the length to be equal to zero
        object = input() #enter the argument 
    
        for i in object:
            count=count+1
    
        print("Length of the string is"+str(count))
    
    length(object)
    
    0 讨论(0)
提交回复
热议问题