>>> sum(map(lambda x:1, "hello world"))
11

>>> sum(1 for x in "foobar")
6

>>> from itertools import count
>>> zip(count(1), "baz")[-1][0]
3

A "tongue twister"

>>> sum(not out not in out for out in "shake it all about")
18

some recursive solutions

>>> def get_string_length(s):
...     return 1 + get_string_length(s[1:]) if s else 0
... 
>>> get_string_length("hello world")
11
>>> def get_string_length_gen(s):
...     yield 1 + next(get_string_length_gen(s[1:])) if s else 0
... 
>>> next(get_string_length_gen("hello world"))
11
>>> 
Answer from John La Rooy on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › find-the-length-of-a-string-without-using-len-function-in-python
Find the Length of a String Without Using len Function in Python | GeeksforGeeks
January 4, 2025 - While Python provides built-in functions len() to determine the length of a string, it's also instructive to explore ways to achieve this without rel
🌐
PREP INSTA
prepinsta.com › home › python program › python program for calculating the length of string without using length() function
length of string without using length - Python Program
October 14, 2022 - Python program for calculating the length of string without using length() function and by iterating through the string.
🌐
Quora
quora.com › Is-it-possible-to-find-the-length-of-a-string-without-using-the-len-function-in-Python
Is it possible to find the length of a string without using the len() function in Python? - Quora
Answer (1 of 3): Yes, you can run a loop for each character in the string, to just count them. But of course, that would be much slower than using the actual len() function. See, in high-level string classes, such as the ones used in Python, ...
🌐
Sanfoundry
sanfoundry.com › python-program-calculate-length-string-without-library
Python Program to Calculate the Length of a String Without using Library Functions - Sanfoundry
May 30, 2022 - The program takes a string and calculates the length of the string without using library functions. ... 1. Take a string from the user and store it in a variable. 2. Initialize a count variable to 0. 3. Use a for loop to traverse through the characters in the string and increment the count variable each time. 4. Print the total count of the variable. 5. Exit. ... Here is source code of the Python Program to calculate the length of a string without using library functions.
🌐
YouTube
youtube.com › watch
4 Ways to Find the Length of a List or String in Python (without the len() Function) TUTORIAL - YouTube
Tutorial on how to calculate the length of lists and strings (without len()) in Python 3.The solutions are:1) For loops2) While loops with slice notation3) S...
Published   April 11, 2020
🌐
YouTube
youtube.com › an it professional
Find Length Of The String without using len() | Python coding interview questions | AnITProfessional - YouTube
This video gives a simple programmatic explanation of how to find the length of the string without using inbuilt method len()Follow our page on FB/Instagram ...
Published   October 20, 2021
Views   2K
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-calculate-the-length-of-a-string-without-using-a-library-function
Python Program to Calculate the Length of a String Without Using a Library Function
my_string = "Hi Will" print("The string is :") print(my_string) my_counter=0 for i in my_string: my_counter=my_counter+1 print("The length of the string is ") print(my_counter)
Find elsewhere
🌐
Learn Coding Fast
learncodingfast.com › home › 2 ways to find length of string in python
2 ways to find length of string in Python | Learn Coding Fast
November 12, 2020 - Next, let’s look at the second approach for finding string length in Python. This approach does not use the built-in len() function.
🌐
BeginnersBook
beginnersbook.com › 2018 › 04 › python-program-to-calculate-length-of-a-string
Python Program to Calculate length of a String
# User inputs the string and it gets stored in variable str str = input("Enter a string: ") # counter variable to count the character in a string counter = 0 for s in str: counter = counter+1 print("Length of the input string is:", counter) ... In the above program we have not used the library function to find length, however we can do the same thing by using a built-in function.
🌐
Newtum
blog.newtum.com › find-the-length-of-a-string-in-python-without-len
Find the Length of a String in Python Without len() - Newtum
November 11, 2024 - Explanation of the Code We’ll break down the logic behind the code used to find the length of a string in Python without len(). This straightforward code leverages a simple loop to count each character in the string.
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-length-of-a-string-in-python-4-ways
Find Length of String in Python - GeeksforGeeks
October 27, 2025 - Each iteration increases the counter by one, giving the total string length. ... Explanation: The loop visits each character once and count += 1 increments the counter per character. The enumerate() function is typically used to loop over an iterable and keep track of both the index and the value of elements within that iterable. ... Explanation: enumerate(a) returns both index (i) and value (ch) for each iteration. Here, we count how many times the loop runs, which equals the string length.
🌐
WsCube Tech
wscubetech.com › resources › python › programs › length-of-string
How to Find Length of a String in Python? (5 Programs)
October 30, 2025 - Learn how to find the length of a string in Python with 5 different programs. Explore multiple methods with examples, outputs, and explanations. Read now!
Top answer
1 of 3
3

You probably need to initialize your variable j (here under renamed counter):

def string_length(my_string):
    """returns the length of a string
    """
    counter = 0
    for char in my_string:
        counter += 1
    return counter

# taking user input
string_input = input("enter string :")
length = string_length(string_input)

print("length is ", length)

This could also be done in one "pythonic" line using a generator expression, as zondo has pointed out:

def string_length(my_string):
    """returns the length of a string
    """
    return sum(1 for _ in my_string)
2 of 3
2

It's quite simple:

def string_length(string):
    return sum(1 for char in string)

1 for char in string is a generator expression that generates a 1 for each character in the string. We pass that generator to sum() which adds them all up. The problem with what you had is that you didn't define j before you added to it. You would need to put j = 0 before the loop. There's another way that isn't as nice as what I put above:

from functools import reduce # reduce() is built-in in Python 2.

def string_length(string):
    return reduce(lambda x,y: x+1, string, 0)

It works because reduce() calls the lambda function first with the initial argument, 0, and the first character in the string. The lambda function returns its first argument, 0, plus one. reduce() then calls the function again with the result, 1, and the next character in the string. It continues like this until it has passed every character in the string. The result: the length of the string.

🌐
CodeChef
codechef.com › learn › course › python › LTCPY08 › problems › PYTH40
String length in Python Programming
Test your Learn Python Programming knowledge with our String length practice problem. Dive into the world of python challenges at CodeChef.
🌐
W3Schools
w3schools.com › python › gloss_python_string_length.asp
Python String Length
Remove List Duplicates Reverse ... Bootcamp Python Certificate Python Training ... To get the length of a string, use the len() function....
🌐
Reddit
reddit.com › r/learnpython › how to input validate the length of a string
r/learnpython on Reddit: How to input validate the length of a string
November 8, 2021 -

Hi I need help I don’t know how to input validate the length of a string so In pseudocode I wanna do this

Input a 3 character name

Check if name is 3 characters

if not then print error so if it’s anything but 3 letters long I wanna say you can’t do that do it again

I know how to do input validation algorithm for integers etc I learned that in school but this assignment is telling me to do something I didn’t learn lol I’ve tried on the internet and it don’t give me an answer

🌐
Java Guides
javaguides.net › 2023 › 09 › python-string-length-without-built-in-functions.html
Python: String Length without Built-in Functions
September 5, 2023 - 1. Create a function to iterate through the string and count the number of characters. ... # Python program to calculate the length of a string without using built-in functions def string_length(s): """Function to calculate the length of a ...