Length method len() is a ready made function available in python. we can use the len() method to find out the length of a given string, tuple, dictionary, array, list, etc. In short notes as per the the python programming, len method returns the length of a given input character. Syntax: len(string) Parameters: Value: the input value which you want the length of. Return value: len() method will return an integer value i.e. the length of a given input like string, or array, or list or collections. Now let’s go through top 5 python examples to find the length. Python Examples for len() Method

Find the string length of a given input in python

The following example code illustrates to find a python string length based on the given input character. Here we are providing the desired the string using variable “input”, so the program calculates the total string length finally as output.
input = "Welcome to Python Tutorials of TraceDynamics"
print("The python string length is :", len(input))

Output:

The python string length is : 44
As per the output, the string length is calculated as 44.

Find the length of a python tuple

Here is the python program which finds the length of a given tuple input character.
tuple = ('Monday','Tuesday','Wednesday')

print("The length of the input tuple is :", len(tuple))

Output:

The length of the input tuple is : 3

Find the length of a python list

Here is the program which finds the length of a python list input.
list = ["Jim","Nick","Rob","Sam"]

print("The length of the input list is :", len(list))

Output:

The length of the input list is : 4

Find the length of a python dictionary

Here is the program which finds the length of a python dictionary input.
dictionary = {'Sam': 12,'Jim':14,'Smith':18,'Rob':28,'Shah':15}

print("The length of the input dictionary is :", len(dictionary))

Output:

The length of the input dictionary is : 5

Find the length of an python array element

array = ['Sam','Mike','Nair']

print("The length of input dictionary is :", len(array))

Output:

The length of the input dictionary is : 3
To conclude, in this python tutorial we gone through top 5 examples to find string length using python len function. Keeping sharing tutorials and happy coding 🙂

Frequently Asked Questions (FAQs)

What is the len() function in Python?

The len() function in Python is used to determine the length of a string, list, tuple, or other iterable types.

How do I find the length of a string in Python?

To find the length of a string in Python, use the len() function like this: len(‘Hello World’).

Can len() be used on data types other than strings?

Yes, len() can also be used on lists, tuples, dictionaries, and other iterable types to get their size or length.

Does len() count spaces in a string?

Yes, len() counts every character in the string, including spaces, punctuation, and special characters.

Is len() efficient for large strings in Python?

Yes, len() is a highly efficient function and can quickly return the length of even very large strings.

Can len() handle Unicode characters in strings?

Yes, len() correctly handles Unicode characters. Each character, including extended Unicode, counts as one.

What happens if I use len() on an empty string?

If len() is used on an empty string, it will return 0.

Can len() be used with nested data structures?

Yes, len() can be used with nested structures, but it will only count the top-level elements.

How do I handle errors with len() in Python?

len() typically doesn’t raise errors unless it’s used on a type that doesn’t support length calculation, like integers.

Is len() a method or a function in Python?

len() is a built-in function in Python, not a method associated with a specific object or class.

Rate this post

Pin It on Pinterest

Share This