This is a text field

Now, you can use the length of the python string

Mystery of length of the life.
The mystery of life is the length

The Great Mystery of life is the length of it

Vanita Vancaspel

Length is the mystery!!

Scientists trying to find the length of the universe, time. but every time they couldn’t find the exact length.

Whereas, In python, length isn’t a mystery.

You can easily find the length of the python string.

Now, I’m going to show you the length of the python string.

Also, I’ll show you how to find the length of the dictionary, tuple and list.

In short, you’re going to see the syntax, parameters and the return value of the length method.

So, stay tuned with me.

First of all, the len() is an inbuilt method.

It takes value. the value could be string, tuple and dictionary.

So, the length method looks like len(value).

len() method for python string parameter

In this section, I’m going to pass the string as the value from the len() method.

It’ll return the number of the integer as the return value.

Return value of the python string length:

The return value will be the number of units.

In short, you will gain the integer value.

Code for len() method

So, let’s see the code for the len() method.

Now I’m going to pass the string as a parameter in the len() method.

For example,

len() method

After, running the code you’ll get:—>

output of len() method

Here, you can see that the return value is 11.

Only 10 alphabets are present here. But the return value is 11.

That means comma(,) counted as a unit.

len() method passing through collections

Now, you’re going to see the different collections passing through the len() method.

In this section, you’re going to see the list, tuple and dictionary pass through the len() method.

len() method for python tuple

You can pass the tuple from the len() method.

The return value will be the number of units in the tuple.

For example,

passing through tuple() Method

After, running the code you’ll get—>

the output of tuple items

Here, you can see that output is the number of tuple items.

So, you can say that tuple returns the number of units.

len() method for list

Now, I’m going to show you how to pass the list from the len() method.

Likewise, tuple it’ll return the number of units in python.

For example,

list passing through len() method

After running the code you’ll see the output:—>

output

From the above example, you can say that list also returns the number of units.

len() method for python dictionary

The last collection that you can pass through len() method is the dictionary.

Likewise, the previous two methods, return the number of units.

For example,

len() method for dictionary

After running the code,

output for len(dictionary)

In the above example, you can see that I’ve passed the dictionary from the len() method.

The return value of the dictionary is the number of units in python.

So, at last, you’d seen the len() method in python.

Internal working of the len() function

Internal working of the len() method

First of all, the len() function takes less or no time to calculate the size of the list, tuple and dictionary.

That’s why it behaves as an o(1) time.

Basically, len() function store a method __len__() that automatically increment as a counter for iterables(tuple, list and dictionary).

That means the return value is stored in the program.

Thus, when you call the len() function. It doesn’t give the command to the interpreter.

Whereas, the len() function returns the stored value.

As a result, it takes less time and follows o(1) time.

For example,

Internal working of the len() function

It returns the number of units.

output of len() method.

Here, you can see that len() function store the __len__() method.

It gives a counter to the iterables such as list now.

That means it takes constant time.

Hence it follows o(1) time complexity.

Conclusion

Finally, you’ve seen the len() method.

The len() method is an inbuilt method.

It takes a value and returns the number of units.

Likewise, it takes collections such as tuples, lists and dictionaries.

This returns the integer data type.

So, what do you think about the len() method? Comment on it!!!

People also reading:

How to replace the python string that saves you 18 seconds

The best way to Loop Using Python Enumerator in [2021]

A new way to format the string in Python 3.6: fstring

Avatar for Vipul kunwar
Follow me

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back to top