Online Tutorials & Training Materials | STechies.com
Register Login

Python String Length len()

|| 0

Python String Length len()
Stechies

How to get the Length of String in Python?

We can use len() function in order Length of string in Python.

Parameter of len() - A list is a defined set in Python.

Syntax of len() - To get the value of the number of elements in a list or length of string python, you must use the syntax len().

Return value of len() - The output prints the length of string as well as number of elements present in the list.

An example of the same would be-

len() function with string.

Input

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

Output

String Length:11

len() function with list

Input

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

Output

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

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

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

Input

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

Output

('The length is :', 14)


Related Articles