Saturday, September 7, 2019

Python String Length | Using len() Function

While programming in python we may need to find what is the length of a string?
Real world data validation may need that you add a check for a username of not more than size 30. Python provides a built-in function len() to find a string's length. len() takes string name as parameter and returns it's length.

Syntax


len(s)

s is the string of which we want to find the length.

Returns

len() returns the number of characters in a string. It is an integer value.

Example


s = "codefantastic"
print(len(s))

Output


13

Note: len() built-in function can also be used to find the number of items of an object passed. Not only string, len() function works with tuple, list, range or bytes also. In fact you can pass collection also to count the number of items present in it.

Further read:

If You Enjoyed This, Take 5 Seconds To Share It