Open In App

Get the Length of Text Without Using Inbuild Python Library Function

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, the length of a string can be calculated without using any library function by iterating through the characters of the string and counting them manually. This process involves creating a counter variable and incrementing it for each character encountered in the string. In this article, we will explore four different approaches to calculating the length of a string without using the library function.

Get the Length of Text Without Using Inbuild Python Library Function

Below are some of the ways by which we can calculate the length of the string without using a library function in Python.

Calculate the Length of the String Using for Loop

The below Python code defines a user-defined function lengthOfString that calculates the length of a given string using a for loop. It iterates through each character in the input string, incrementing a counter variable (length) for each character, and returns the final count as the length of the string.

Python3




# input string
string = "GeeksForGeeks"
 
# user-defined function
 
 
def lengthOfString(String):
    length = 0
    # for loop to calculate length of string
    for char in String:
        length += 1
    return length
 
 
# printing output
print("Length of the string:", lengthOfString(string))


Output

Length of the string: 13




Time complexity: O(n)
Space complexity: O(1)

Calculate the Length of the String Using lambda Function

The below Python code defines a user-defined function length_of_string that calculates the length of a given string using lambda function and mapping. We can use map and lambda to create a list of 1’s with the same length as the string and then use the sum function to add up those 1’s, giving us the length of the string.

Python3




# input string
string = "GeeksForGeeks"
 
# function defined
def length_of_string(string):
    # creating a list of 1's with the same length as the string using map and lambda
    ones_list = list(map(lambda x: 1, string))
 
    # calculating the length of the string by summing up the elements in the ones_list
    length = sum(ones_list)
    return length
 
 
# printing the length of the string
print("Length of the string:", length_of_string(string))


Output

Length of the string: 13




Space complexity: O(n)
Time complexity: O(n)

Calculate the Length of the String Using enumerate Function

The below code defines a function length_of_string_enumerate that calculates the length of the given string “GeeksforGeeks” using the enumerate function in a loop. The output is then printed, showing the length of the string as 13.

Python3




# Using enumerate
string = "GeeksforGeeks"
 
# defining function
def length_of_string_enumerate(string):
    count = 0
    # using enumerate to calculate length
    for _ in enumerate(string):
        count += 1
    return count
 
 
result = length_of_string_enumerate(string)
# printing output
print("Length of the string:", result)


Output

Length of the string: 13




Space Complexity: O(n)

Time complexity: O(1)

Calculate the Length of the String Using Recursion

The below Python code defines a user-defined function lengthOfString that calculates the length of a given string using recursion. This function returns a value that calls itself until the index has not reached its limit.

Python3




# input string
string = "GeeksForGeeks"
 
def lengthOfString(String, index=0):
    # if the current index is at the end of the string, return 0
    if not String[index:]:
        return 0
    # increment the length and call the function with the next index
    return 1 + lengthOfString(String, index + 1)
 
 
  # printing output
print("Length of the string:", lengthOfString(string))


Output

Length of the string: 13




Time Complexity: O(n)

Space complexity: O(n)

Conclusion

In conclusion, these four approaches provide different methods for calculating the length of a string in Python without using library functions. The for loop and lambda function approaches offer simplicity and efficiency, with O(n) time complexity. The enumerate function demonstrates a concise way to calculate the length with O(1) space complexity. The recursive approach, while clear in concept, comes with higher space complexity (O(n)) due to the recursive calls



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads