Online Tutorials & Training Materials |
Register Login

Length of String in Python

|| 0

Length of String in Python

What is len() in python?

len() function in python is an inbuilt function which returns length (number of character) of string in Python.

Parameter of len() Function Python

len() function takes string or list of elements as a parameter

Syntax of len() Function Python

Syntax of len() is:


Return Value of len() Function Python

The output prints the length of the string or number of elements present in the list.

Note: If an invalid argument is passed then a TypeError Exception will be raised

An example of the same would be-

How to find length of string in Python?

Please follow the example below to get the length of string using len().

stringlenth = 'Stechies'
print("String Length: ",len(stringlenth))


String Length: 8

How to find the number of elements in a list in Python?

listB, listD = [9977, 'xyz', 'sahara', 'stechies'], [459, 'pqr', 234]
print("Length of list B : ", len(listB))
print("Length of list D : ", len(listD))


Length of list B :  4
Length of list D :  3

Calculate length of string without using len() function in Python

However, if you are interested to find the length of a string without using len() function, you can follow this example instead.

def string_length(s):   
    if s == '': return 0
    return 1 + string_length(s[1:])
len = string_length("Enter String :")
print("The length is :", len)


('The length is :', 14)