{"id":4585,"date":"2020-03-30T16:08:51","date_gmt":"2020-03-30T16:08:51","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4585"},"modified":"2023-02-16T19:57:08","modified_gmt":"2023-02-16T19:57:08","slug":"python-f-string","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-f-string","title":{"rendered":"Understanding Python f-string"},"content":{"rendered":"\n<p>String formatting in Python can be achieved using f-string. Thus, in this article, we will be focusing on the implementation of Python f-string.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Necessity of f-string in Python<\/h2>\n\n\n\n<p>Python f-string basically serves the purpose of string formatting. Before the emergence of &#8220;f-strings&#8221;, we had the following ways to format strings in Python:<\/p>\n\n\n\n<p><strong>1.<\/strong> <strong>Python &#8216;%&#8217; operator<\/strong> &#8212; <\/p>\n\n\n\n<p><strong>Disadvantage<\/strong>: Python <code>% operator<\/code> cannot be used with Objects and attributes.<\/p>\n\n\n\n<p><strong>2. <\/strong>Python format() function &#8212;<\/p>\n\n\n\n<p><strong>Disadvantage<\/strong>: The <code>string.format() function<\/code> could overcome the drawback of &#8216;%&#8217; operator, but it proved out to be a <strong>verbose <\/strong>way of formatting.<\/p>\n\n\n\n<p>Thus, <strong>Python f-string <\/strong>came into existence wherein <strong>the strings can be interpolated and formatted with much simpler and minimal syntax<\/strong>. The <strong>strings are formatted at runtime<\/strong> by the Python interpreter.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Working of Python f-string with examples<\/h2>\n\n\n\n<p>The f-string also known as formatted strings serves the purpose of <strong>Literal String Interpolation <\/strong>i.e. injection of strings and formatting of the particular string.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf&#039;{string}&#039;\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong> <strong>f-string with string as an iterable<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr1 = &#039;Python&#039;\n\nstr4 = &#039;JournalDev&#039;\n\nres = f&#039;{str4} provides tutorials on {str1}&#039;\n\nprint(res)\n<\/pre><\/div>\n\n\n<p>As clearly seen above, f-string is used to <strong>inject <\/strong>or <strong>interpolate <\/strong>the input string <strong>str1 <\/strong>and <strong>str4 <\/strong>between the string statement.<\/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=\"\">\nJournalDev provides tutorials on Python\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Python f-string with raw strings<\/h3>\n\n\n\n<p>Python raw strings basically treat the special characters considered as <strong>&#8216;escape sequences&#8217; as literal characters<\/strong>. It is used when we want the escape sequences i.e.<strong> &#8216;\\n&#8217;<\/strong> or<strong> backslash(\\)<\/strong> as literal sequences of characters.<\/p>\n\n\n\n<p><strong>Syntax: Python raw strings<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nr&#039;string&#039;\n<\/pre><\/div>\n\n\n<p>Python f-strings can work well simultaneously with the raw strings.<\/p>\n\n\n\n<p><strong>Syntax: f-string along with raw strings<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfr&#039;string or {string}&#039;\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr1 = &#039;Python&#039;\n\nstr4 = &#039;JournalDev&#039;\n\nres = fr&#039;{str4} \\n and AskPython provides tutorials on {str1}&#039;\n\nprint(res)      \n\n<\/pre><\/div>\n\n\n<p>In the above example, &#8216;\\n&#8217; is treated as a literal character.<\/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=\"\">\nJournalDev \\n and AskPython provides tutorials on Python\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Calling functions with f-string <\/h3>\n\n\n\n<p>Python f-strings enables us to call <a href=\"https:\/\/www.askpython.com\/python\/python-functions\" class=\"rank-math-link\">functions<\/a> within it. Thus, optimizing the code to an extent. The same way can be used for creating lambda functions within f-string bracets.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf&#039;{func()}&#039;\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef mult(a,b):\n    res = a*b\n    return res\n\nmult_res = f&#039;{mult(10,20)}&#039;\nprint(mult_res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n200\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Python f-string with blank\/white-spaces<\/h3>\n\n\n\n<p>Python f-strings can also work with blank or <strong>white-spaces<\/strong>. It <strong>ignores the trailing and the leading white-spaces <\/strong>and the <strong>spaces between the literal string are unaltered<\/strong> and <strong>preserved<\/strong>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmult_res = f&#039;  Multiplication: { 10 * 10 }  &#039;\nprint(mult_res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n  Multiplication: 100  \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Python f-string with expressions<\/h3>\n\n\n\n<p>Python f-string can work with <strong>expressions<\/strong>. Thus, basic manipulations can be performed directly within f-string.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf&#039;{expression&#039;}\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = 10\ny = 5\nprint(f&#039;Result: {x\/y} &#039;)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nResult: 2.0 \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">f-string with a Python dictionary<\/h3>\n\n\n\n<p>As we all know, <a href=\"https:\/\/www.askpython.com\/python\/dictionary\" class=\"rank-math-link\">Python dictionary<\/a> data structure works with key-value pairs. Python f-string can also be framed along with the dictionaries.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf&quot;{dict&#x5B;&#039;key&#039;]}&quot;\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninfo = {&#039;age&#039;:&#039;21&#039;, &#039;city&#039;:&#039;Pune&#039;}\n\nprint(f&quot;{info&#x5B;&#039;city&#039;]}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nPune\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the necessity and working of f-string with various iterables and expressions.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python f-string &#8212; JournalDev<\/li><li><a href=\"https:\/\/peps.python.org\/pep-0498\/\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Literal String Interpolation &#8212; PEP 498<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>String formatting in Python can be achieved using f-string. Thus, in this article, we will be focusing on the implementation of Python f-string. Necessity of f-string in Python Python f-string basically serves the purpose of string formatting. Before the emergence of &#8220;f-strings&#8221;, we had the following ways to format strings in Python: 1. Python &#8216;%&#8217; [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":4606,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4585","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\/4585","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4585"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4606"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}