How to Calculate String Length Without Build-in Function in Python


In this example, we will learn how to count the length of a string without using a library function.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

str = input("Please input the string: ")
length = 0
for i in str:
    length += 1
print("The length is: ", length)

Output:

Please input the string: Hello World
The length is: 11
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement