How to grab the length of the python string?
In physical world, you do measurement of objects.
You measure the length of the hair, chair and table.
Likewise, in python you can measure the length of the python string.
Thus, I'm moving to show you how to grab the length of the python string.
So, let's dive into it step by step.
Use the len() method
First of all, the syntax of length method is len(string).
Second, you can observe the passed parameter is string data type.
Third, the return value is integer.
After running the code you'll get:--->
Now, you can see that the return value is integer.
Also, the len() method count the whitespace.
That means if there's a whitespace in a string it has a length also.
After, the code runs it gives the output--->
Here, you can see that only 10 alphabets in Hello world. But between Hello and world there's a whitespace.
So, that's why return value is 11.
The len() method in tuple
First of all, A tuple method contains the data items.
That means, the len() method returns the number of items in tuple.
And this items are different data type.
When above code runs it gives the below output--->
In above example, you can see that "hello" has 5 alphabets. But the len() method count it as 1 item.
Because, there are another elements with it.
Here 3 elements are present. so, the return value is 3.
##The len() method in dictionary
You can also use length method in dictionary.
The dictionary is a key-value pair method.
Thus, one key-value pair has length of 1.
When above code runs, you'll get below output--->
This is the length of the python string in dictionary.
Note: Here, I've only used string data type in dictionary.
Why should you use the len() method?
This question is a common one.
So, below are two cases which give you quick solution to your question.
Case1: In students list
Suppose, during admission process you've to find the number of student taken the admission.
At that time, you'll not going to count every student.
If you do, it take lots of time.
Whereas, you can simply pass the variable to the len() method and grab how many students have taken the admission.
So, using the len() method giving you quick solution.
Case2: Use it to find the number of clothes sale.
Imagine you're a cloth owner.
You've created a list of clothes you've sold in a month.
But you want to find the number of the clothe you've sold.
That time you can use len() method on list. using At last, I've shown you two examples. There are many other examples, where you can use it.
Basically, len() method is useful in finding the numbers of data you're using in program.
Finally, you've seen the length of python string.
The len() method makes it possible.
Also, the len() method include the whitespace.
That means whitespace has length.
You can use len() method in dictionary, tuple and other iterables.
So, In python you can measure the string.
This will help you to find the quantity of code.
Thus, what you're waiting for, use it.
So, what do you think about len() method?
Comment on it!!!
Also, Subscribe to my newsletter. so you can get fresh python topics.