{"id":8637,"date":"2021-02-26T17:13:20","date_gmt":"2021-02-26T11:43:20","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8637"},"modified":"2021-02-26T17:13:22","modified_gmt":"2021-02-26T11:43:22","slug":"using-the-len-function-in-python-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/using-the-len-function-in-python-with-examples\/","title":{"rendered":"Using the len() Function in Python with Examples"},"content":{"rendered":"\n<p>The len() function is an <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-functions-examples\/\" class=\"rank-math-link\">inbuilt python function<\/a> used to return the number of characters in a string or the number of items in a collection such as a list, tuple, dictionary etc.\u00a0<\/p>\n\n\n\n<p>Using the function has a lot of use cases when writing day to day codes. You may need to check the length of an object during iterations and what have you. In this tutorial, we will take several examples on how to use the len function in different Python objects.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s begin with its syntax<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Signature: len(obj, \/)\nDocstring: Return the number of items in a container.\nType:\u00a0 \u00a0 \u00a0 builtin_function_or_method\n<\/pre>\n\n\n\n<p>Parameters: The object whose length wants to be returned. The object could be a string, list, tuple, dictionary etc.&nbsp;<\/p>\n\n\n\n<p>Returns: The length of the object given<\/p>\n\n\n\n<p>Let&#8217;s take some examples.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Finding the length of a String<\/h2>\n\n\n\n<p>The len() function returns the length of a string. It is important to note that every character in the string would be counted including whitespace, punctuations and any other type of special characters.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the length of a string\u00a0\n\u00a0\nstring = \"I am learning Python with H2k Infosys\"\n\u00a0\nprint(f\"The string has {len(string)} characters\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe string has 37 characters\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Finding the length of a List&nbsp;<\/h2>\n\n\n\n<p>The len function returns the number of elements in a list. See the example below.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the length of a list\u00a0\n\u00a0\n# string = \"I am learning Python with H2k Infosys\"\nnames = ['Mike', 'Josh', 'Ope', 'Toby', 'Fred', 'Krish']\n\u00a0\nprint(f\"The list has {len(names)} items\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe list has 6 items\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Finding the length of a Tuple<\/h2>\n\n\n\n<p>A tuple is a container that stores a collection of items, like in a list. The major difference between a tuple and a list is that the element in a tuple cannot be changed. They are immutable. You can also find the number of elements in a tuple by using the len() function. See the example below.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the length of a list\u00a0\n\u00a0\n# string = \"I am learning Python with H2k Infosys\"\nnames = ('Mike', 'Josh', 'Ope', 'Toby', 'Fred', 'Krish')\n\u00a0\nprint(f\"The object is of datatype, {type(names)}\")\nprint(f\"The tuple has {len(names)} items\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe object is of datatype, &lt;class 'tuple'>\nThe tuple has 6 items\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Finding the length of a Dictionary<\/h2>\n\n\n\n<p>The len function can be used to count the length of a dictionary. Every key-value pair is seen as one unit.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the length of a list\u00a0\n\u00a0\n# string = \"I am learning Python with H2k Infosys\"\ninfo = {'names': ['Mike', 'Vic', 'Ope', 'Toby', 'Mary', 'Krish'],\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'age': [12, 32, 20, 21, 43, 25],\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0'gender': ['M', 'M', 'M', 'F', 'F', 'M']}\n\u00a0\nprint(f\"The object is of datatype, {type(info)}\")\nprint(f\"The tuple has {len(info)} items\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe object is of datatype, &lt;class 'dict'>\n<\/code><\/pre>\n\n\n\n<p>Notice that the length of the dictionary is given as 3 items even though the values for each key is a list of 6 items. Again, a key-value pair is seen as one item. There were 3 key-value pairs in the dictionary, names, age and age. This is why the len function returns 3 for the dictionary.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">In summary<\/h2>\n\n\n\n<p>You have seen how to use the len() function for various data types and <a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">structures in Python<\/a>. It can come handy when you wish to loop over the number of items in an iterator or even in simple cases of you finding how many items the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/working-with-strings-in-python\/\" class=\"rank-math-link\">iterator contains.<\/a> If you have any questions, feel free to leave them in the comment section and I\u2019d do my best to answer them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The len() function is an inbuilt python function used to return the number of characters in a string or the number of items in a collection such as a list, tuple, dictionary etc.\u00a0 Using the function has a lot of use cases when writing day to day codes. You may need to check the length [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8637"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8637\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8643"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}