{"id":2887,"date":"2020-01-29T06:01:59","date_gmt":"2020-01-29T06:01:59","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2887"},"modified":"2023-02-16T19:57:17","modified_gmt":"2023-02-16T19:57:17","slug":"find-string-length-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/find-string-length-in-python","title":{"rendered":"Find String Length in Python"},"content":{"rendered":"\n<p>We can find <a href=\"https:\/\/www.askpython.com\/python\/python-data-types#python-string\" class=\"rank-math-link\">string<\/a> length in Python using the built-in <code>len()<\/code> function. Let&#8217;s look at how this function works, and also let&#8217;s try finding the length of various types of Python strings using <code>len()<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using len()<\/h2>\n\n\n\n<p>Let&#8217;s look at some simple examples to illustrate <code>len()<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a = &quot;Hello from AskPython&quot;\n&gt;&gt;&gt; print(len(a))\n20\n<\/pre><\/div>\n\n\n<p>This prints 20 because it is the number of characters in the string. We can thus find the length using <code>len()<\/code>.<\/p>\n\n\n\n<p>Even when the string has special characters, as long as it can be encoded in some Unicode format, we can calculate it&#8217;s length.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a = &#039;A\u00e5B\u00e7&#039;\n&gt;&gt;&gt; print(len(a))\n4\n<\/pre><\/div>\n\n\n<p>For strings with special escape characters (they are prefixed by a backslash<code>(\\)<\/code>, only the character is counted towards the length and not the backslash. Examples include (<code>\\n<\/code>, <code>\\t<\/code>, <code>\\'<\/code>, etc)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; a = &#039;A\\t\\t&#039;\n&gt;&gt;&gt; print(len(a))\n3\n\n&gt;&gt;&gt; b = &#039;A\\n\\nB&#039;\n&gt;&gt;&gt; print(len(b))\n4\n\n&gt;&gt;&gt; c = &#039;A\\&#039;B&#039;\n&gt;&gt;&gt; print(len(c))\n3\n<\/pre><\/div>\n\n\n<p>For raw strings, since they treat backslash (<code>\\<\/code>) as literal, the backslash will be counted towards the length of the string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; s = r&#039;A\\t\\t&#039;\n&gt;&gt;&gt; print(len(s))\n5\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Working of len()<\/h2>\n\n\n\n<p>When we call the <code>len()<\/code> function using the String object, the <code>__len__()<\/code> method of the String object is called.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt; a = &quot;Hello from AskPython&quot;\n&gt;&gt;&gt; a.__len__()\n20\n<\/pre><\/div>\n\n\n<p>To prove this, let us implement our own <code>len()<\/code> on a custom Class. Since <code>__len__()<\/code> works on objects, we must inherit the class <code>object<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Student(object):\n    def __init__(self, name):\n        self.name = name\n\n    def __len__(self):\n        print(&quot;Invoking the __len__() method on the Student Object to find len()...&quot;)\n        count = 0\n        for i in self.name:\n            count += 1\n        return count\n\n\na = Student(&quot;Amit&quot;)\nprint(len(a))\n<\/pre><\/div>\n\n\n<p>Since the <code>len()<\/code> method invokes <code>__len__()<\/code>, we will go through the function, which counts the number of objects in the iterable. Since we pass a string, we will simply get the length, which will come out to be 4!<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nInvoking the __len__() method on the Student Object to find len()...\n4\n<\/pre><\/div>\n\n\n<p>We have thus implemented our own <code>len()<\/code> method for the class <code>Student<\/code>! Amazing, isn&#8217;t it?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>JournalDev article on String Length<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>We can find string length in Python using the built-in len() function. Let&#8217;s look at how this function works, and also let&#8217;s try finding the length of various types of Python strings using len(). Using len() Let&#8217;s look at some simple examples to illustrate len(). This prints 20 because it is the number of characters [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,3],"tags":[],"class_list":["post-2887","post","type-post","status-publish","format-standard","hentry","category-built-in-methods","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2887"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2887\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}