{"id":12888,"date":"2020-05-19T01:44:20","date_gmt":"2020-05-19T08:44:20","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=12888"},"modified":"2023-12-01T02:47:38","modified_gmt":"2023-12-01T10:47:38","slug":"python-len","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-len\/","title":{"rendered":"A Step-By-Step Guide to Len Python"},"content":{"rendered":"\n<p><em>The <\/em><code><em>len()<\/em><\/code><em> Python method returns the length of a list, string, dictionary, or any other iterable data format in Python. The <\/em><code><em>len()<\/em><\/code><em> method takes one argument: an iterable object. <\/em><code><em>len()<\/em><\/code><em> is a built-in method in Python. <\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>When programming, you may encounter a scenario where you want to acquire the length of a particular iterable object. For example, you may have a list of orders made by customers at your car dealership, which you want to use to calculate the average car price. In order to do so, you\u2019ll first need to know how many cars were sold.<\/p>\n\n\n\n<p>That\u2019s where the <code>len()<\/code> method in Python comes in. The Python <code>len()<\/code> method is a built-in function that can be used to calculate the length of any iterable object. So, if you want to get the length of a string, list, dictionary, or another iterable object in Python, the <code>len()<\/code> method can be useful.<\/p>\n\n\n\n<p>In this tutorial, we are going to explore how to use the <code>len()<\/code> method in Python. We\u2019ll also run through a few examples of <code>len()<\/code> being used on strings, lists, and dictionaries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Len Python<\/h2>\n\n\n\n<p>The <code>len()<\/code> function returns the number of items in an iterable object. Iterable objects such as lists and strings are indexed, which means that each value in the object is assigned an index value. This index value can be used to reference individual values in an object.<\/p>\n\n\n\n<p>The <code>len()<\/code> method takes in one argument: the iterable object of which you would like to calculate the length. Here\u2019s the syntax of the <code>len()<\/code> method in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>len(iterable_object)<\/pre><\/div>\n\n\n\n<p>Let\u2019s go through an example to illustrate how this method works. Say that you are operating a car dealership and you want to know how many cars were sold last month. You already have a list of sales, but you want to find out how many items are in that list. You could use the following code to accomplish this task: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>last_month_sales = [\"Vauxhall Corsa 2011\", \"Nissan Note 2014\", \"Honda Civic 2012\", \"Vauxhall Corsa 2015\", \"Vauxhall Mokka 2013\", \"Hyundai I20 2016\"]\nprint(len(last_month_sales))<\/pre><\/div>\n\n\n\n<p>Our code returns an integer value which represents the length of our list: 6. On the first line of our program, we declared a variable called <code>last_month_sales<\/code> which stores all of the sales made last month at the dealership. Then, we use <code>len()<\/code> to calculate the length of our array, and we print out that value to the console using <code>print()<\/code>.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python String Length<\/h2>\n\n\n\n<p>In addition, we could use <code>len()<\/code> to get the length of a string. Let\u2019s say that you are building a speed-typing game and you want to know how many characters a player has typed. You could use the following code to accomplish this goal:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>player_one_output = \"The quick brown fox jumped over the lazy dog. Indeed, he did, and it was great to witness.\"\nprint(len(player_one_output))<\/pre><\/div>\n\n\n\n<p>Our code returns: 90. In Python, strings are indexed, which means each character \u2014 including spaces and symbols \u2014 is given its own value. So, the <code>len()<\/code> function counts how many items (or characters) are in our string, which in this case is 90.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Len in For Loop<\/h2>\n\n\n\n<p>The <code>len()<\/code> method is often used with a <code>for<\/code> loop in Python to instruct the loop how many times to execute a particular block of code.<\/p>\n\n\n\n<p>For example, let\u2019s say that we want to print out a list of every car sold by our car dealership last month, and each value should be on a new line. We could do this using the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>last_month_sales = [\"Vauxhall Corsa 2011\", \"Nissan Note 2014\", \"Honda Civic 2012\", \"Vauxhall Corsa 2015\", \"Vauxhall Mokka 2013\", \"Hyundai I20 2016\"]\nfor car in range(0, len(last_month_sales)):\n\tprint(last_month_sales[car])<\/pre><\/div>\n\n\n\n<p>Our code uses <code>len()<\/code> to calculate the number of cars sold last month, and then the <code>for<\/code> loop uses this value to loop through each car in the <code>last_month_sales<\/code> array. Here\u2019s the output of our code: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Vauxhall Corsa 2011\nNissan Note 2014\nHonda Civic 2012\nVauxhall Corsa 2015\nVauxhall Mokka 2013\nHyundai I20 2016<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <code>len()<\/code> method in Python can be used to calculate the length of an iterable object. For instance, <code>len()<\/code> could be used to calculate how many students are in a specific class at school, or how many donuts a local donut shop has sold over the last week.<\/p>\n\n\n\n<p>In this tutorial, we discussed how to use the <code>len()<\/code> method in Python on an iterable object. We also explored how <code>len()<\/code> can be used with a <code>for<\/code> loop to iterate through an iterable object. Now you have the information you need to start using Python\u2019s built-in <code>len()<\/code> method like an expert!<\/p>\n\n\n\n<p><strong><em>Python is used for a variety of purposes in the tech industry. Download the <\/em><\/strong><a href=\"https:\/\/careerkarma.com\/\"><strong><em>free Career Karma app<\/em><\/strong><\/a><strong><em> to talk with one of our coaches about how learning Python could help you break into your dream role in tech.<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"The len() Python method returns the length of a list, string, dictionary, or any other iterable data format in Python. The len() method takes one argument: an iterable object. len() is a built-in method in Python. When programming, you may encounter a scenario where you want to acquire the length of a particular iterable object.&hellip;","protected":false},"author":240,"featured_media":12889,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-12888","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>A Step-By-Step Guide to Len Python | Career Karma<\/title>\n<meta name=\"description\" content=\"The len() method is used by Python developers to find the length of an iterable object. Learn how to use the len() method on Career Karma.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-len\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Step-By-Step Guide to Len Python\" \/>\n<meta property=\"og:description\" content=\"The len() method is used by Python developers to find the length of an iterable object. Learn how to use the len() method on Career Karma.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-len\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-19T08:44:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:47:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"A Step-By-Step Guide to Len Python\",\"datePublished\":\"2020-05-19T08:44:20+00:00\",\"dateModified\":\"2023-12-01T10:47:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/\"},\"wordCount\":703,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-len\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-len\/\",\"name\":\"A Step-By-Step Guide to Len Python | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg\",\"datePublished\":\"2020-05-19T08:44:20+00:00\",\"dateModified\":\"2023-12-01T10:47:38+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The len() method is used by Python developers to find the length of an iterable object. Learn how to use the len() method on Career Karma.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-len\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg\",\"width\":1000,\"height\":667,\"caption\":\"Close-up of a computer screen\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-len\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"A Step-By-Step Guide to Len Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"A Step-By-Step Guide to Len Python | Career Karma","description":"The len() method is used by Python developers to find the length of an iterable object. Learn how to use the len() method on Career Karma.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-len\/","og_locale":"en_US","og_type":"article","og_title":"A Step-By-Step Guide to Len Python","og_description":"The len() method is used by Python developers to find the length of an iterable object. Learn how to use the len() method on Career Karma.","og_url":"https:\/\/careerkarma.com\/blog\/python-len\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-05-19T08:44:20+00:00","article_modified_time":"2023-12-01T10:47:38+00:00","og_image":[{"width":1000,"height":667,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-len\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-len\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"A Step-By-Step Guide to Len Python","datePublished":"2020-05-19T08:44:20+00:00","dateModified":"2023-12-01T10:47:38+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-len\/"},"wordCount":703,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-len\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-len\/","url":"https:\/\/careerkarma.com\/blog\/python-len\/","name":"A Step-By-Step Guide to Len Python | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg","datePublished":"2020-05-19T08:44:20+00:00","dateModified":"2023-12-01T10:47:38+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The len() method is used by Python developers to find the length of an iterable object. Learn how to use the len() method on Career Karma.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-len\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-len\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-len\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/close-up-of-computer-screen-325111.jpg","width":1000,"height":667,"caption":"Close-up of a computer screen"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-len\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"A Step-By-Step Guide to Len Python"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/12888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=12888"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/12888\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/12889"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=12888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=12888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=12888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}