Get the Length of Text Without Using Inbuild Python Library Function
Last Updated :
08 Feb, 2024
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
string = "GeeksForGeeks"
def lengthOfString(String):
length = 0
for char in String:
length + = 1
return length
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
string = "GeeksForGeeks"
def length_of_string(string):
ones_list = list ( map ( lambda x: 1 , string))
length = sum (ones_list)
return length
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
string = "GeeksforGeeks"
def length_of_string_enumerate(string):
count = 0
for _ in enumerate (string):
count + = 1
return count
result = length_of_string_enumerate(string)
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
string = "GeeksForGeeks"
def lengthOfString(String, index = 0 ):
if not String[index:]:
return 0
return 1 + lengthOfString(String, index + 1 )
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
Share your thoughts in the comments
Please Login to comment...