Write your own len () in Python

Counters | Python Methods and Functions

How to implement without using len ():

 1. Take input and pass the string / list into a function which return its length. 2. Initialize a count variable to 0, this count variable count character in the string. 3. Run a loop till length if the string and increment count by 1. 4. When loop is completed return count. 

Python implementation:

# Function that returns line length

def findLength (string):

 

# Initialize counter to zero

count = 0

 

# Symbol count in the line

for i in string:

co unt + = 1

# Returned amount

return count

 
# Driver code

string = "pythonengineering"

print (findLength (string))

Output:

 13 

This article provided by Sahil Rajput . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please write in comments if you find anything wrong or if you'd like to share more information on the topic discussed above.





Tutorials