{"id":4305,"date":"2020-03-26T13:52:37","date_gmt":"2020-03-26T13:52:37","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4305"},"modified":"2022-08-06T13:12:34","modified_gmt":"2022-08-06T13:12:34","slug":"python-string-casefold","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-string-casefold","title":{"rendered":"Python String casefold()"},"content":{"rendered":"\n<p>The <a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-functions\" class=\"rank-math-link\">Python String<\/a> casefold() method returns a case folded string, and we use this to remove all case distinctions in a string.<\/p>\n\n\n\n<p>Let&#8217;s look at what this means.<\/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 the Python String casefold() method<\/h2>\n\n\n\n<p>This belongs to the <code>String<\/code> class, and can be used only on String objects.<\/p>\n\n\n\n<p>The Syntax for this method is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring_obj.casefold()\n<\/pre><\/div>\n\n\n<p>This will return a new string, after applying all case folds.<\/p>\n\n\n\n<p>For example, it will convert all uppercase letters to lowercase.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_str = &quot;Hello from AskPython&quot;\n\ncasefolded_str = my_str.casefold()\n\nprint(casefolded_str)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhello from askpython\n<\/pre><\/div>\n\n\n<p>This converts all uppercase letters to lowercase ones for the English alphabet.<\/p>\n\n\n\n<p>But what if the string has characters from another language, and in another encoding? The Python string casefold() method solves this problem.<\/p>\n\n\n\n<p>Take an example of the German lowercase letter &#8216;<code>\u00df<\/code> &#8216;. This corresponds to the English string &#8220;ss&#8221;, since there is an encoding for German alphabets, and we decode it to an English string.<\/p>\n\n\n\n<p>If we were to use the <code>lower()<\/code> method on this alphabet, it would do nothing, since the letter is already in lowercase, so the output will remain &#8216; <code>\u00df<\/code> &#8216;.  Python string casefold() not only converts to lowercase, but also makes sure we get back our English string &#8220;ss&#8221;.<\/p>\n\n\n\n<p>The below screenshot shows this difference.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"694\" height=\"209\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/03\/german_string_casefold.png\" alt=\"German String Casefold\" class=\"wp-image-4308\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/03\/german_string_casefold.png 694w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/03\/german_string_casefold-300x90.png 300w\" sizes=\"auto, (max-width: 694px) 100vw, 694px\" \/><figcaption>German String Casefold<\/figcaption><\/figure><\/div>\n\n\n\n<p>Let&#8217;s take one more example, to show that the strings &#8216;<code>\u00df<\/code>&#8216; and &#8220;SS&#8221; will resolve to the same casefolded string &#8220;ss&#8221;.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ns1 = &#039;\u00df&#039;\ns2 = &#039;ss&#039;\ns3 = &#039;SS&#039;\nif s1.casefold() == s2.casefold():\n    print(&#039;Casefolded strings of s1 and s2 are equal&#039;)\nelse:\n    print(&#039;Casefolded strings of s1 and s2 are not equal&#039;)\n\nif s1.casefold() == s3.casefold():\n    print(&#039;Casefolded strings of s1 and s3 are equal&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCasefolded strings of s1 and s2 are equal\nCasefolded strings of s1 and s3 are equal\n<\/pre><\/div>\n\n\n<p>Indeed, both these strings resolve to the same casefolded string!<\/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\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned how we could use the Python string casefold()  method in Python.<\/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><a href=\"https:\/\/docs.python.org\/3.7\/library\/stdtypes.html#str.casefold\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python Ofiicial Documentation<\/a> on casefold()<\/li><li> JournalDev article on String casefold() <\/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>The Python String casefold() method returns a case folded string, and we use this to remove all case distinctions in a string. Let&#8217;s look at what this means. Using the Python String casefold() method This belongs to the String class, and can be used only on String objects. The Syntax for this method is: This [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":4314,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4305","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=4305"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4305\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4314"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}