{"id":5623,"date":"2019-09-30T13:03:06","date_gmt":"2019-09-30T18:03:06","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=5623"},"modified":"2019-09-30T13:03:06","modified_gmt":"2019-09-30T18:03:06","slug":"python-f-strings","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=5623","title":{"rendered":"Python f-strings"},"content":{"rendered":"<p>I&#8217;m accustomed to using variables directly in strings &#8212; most of my scripting experience was with Bash, Perl, and PHP. It&#8217;s something I have to think about when I would write Java or C++ code, and it&#8217;s one of the few things that struck me as a step backward in Python. This is basically the advantage that kwargs have over args, except changing a string common where changing a function definition is pretty rare.<\/p>\n<p>I&#8217;ve taken to using the arg identifier numbers in the print statement.<\/p>\n<p>&gt;&gt;&gt; first=&#8221;Lisa&#8221;<br \/>\n&gt;&gt;&gt; last=&#8221;Rushworth&#8221;<br \/>\n&gt;&gt;&gt; print(&#8220;Hi {0}, you are {0} {1}.&#8221;.format(first, last))<br \/>\nHi Lisa, you are Lisa Rushworth.<\/p>\n<p>This allows me to append a new variable to the format argument list and use it anywhere in the string<br \/>\n&gt;&gt;&gt; salutation=&#8221;Hello&#8221;<br \/>\n&gt;&gt;&gt; print(&#8220;{2} {0}, you are {0} {1}.&#8221;.format(first, last, salutation))<br \/>\nHello Lisa, you are Lisa Rushworth.<\/p>\n<p><a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0502\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python 3 introduced f-strings<\/a>, which go beyond the functionality I was missing. You *can* use variable names within the string:<\/p>\n<p>&gt;&gt;&gt; print(f&#8221;{salutation} {first}, you are {first} {last}&#8221;)<br \/>\nHello Lisa, you are Lisa Rushworth<\/p>\n<p>You can have multi-line strings &#8212; notice that newlines aren&#8217;t being magically added. If you *want* newlines, add \\n!<\/p>\n<p>&gt;&gt;&gt; strString = (<br \/>\n&#8230; f&#8221;{salutation} {first}, &#8221;<br \/>\n&#8230; f&#8221;You are {first} {last} &#8221;<br \/>\n&#8230; f&#8221;And I have more &#8221;<br \/>\n&#8230; f&#8221;data down here&#8221;<br \/>\n&#8230; )<br \/>\n&gt;&gt;&gt; print(strString)<br \/>\nHello Lisa, You are Lisa Rushworth And I have more data down here<\/p>\n<p>But you can also perform operations:<br \/>\n&gt;&gt;&gt; print(f&#8221;{2 ** 3}&#8221;)<br \/>\n8<\/p>\n<p>And use the f-string in classes<br \/>\n&gt;&gt;&gt; class MyThing:<br \/>\n&#8230; def __init__(self, thing1, thing2, thing3):<br \/>\n&#8230; self.thing1 = thing1<br \/>\n&#8230; self.thing2 = thing2<br \/>\n&#8230; self.age = thing3<br \/>\n&#8230;<br \/>\n&#8230; def __str__(self):<br \/>\n&#8230; return f&#8221;{self.thing1} has 2 {self.thing2} and 3 {self.age}.&#8221;<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt;<br \/>\n&gt;&gt;&gt; thingSample = MyThing(&#8220;Something1&#8221;, &#8220;Something2&#8221;, &#8220;Something3&#8243;)<br \/>\n&gt;&gt;&gt; print(f&#8221;{thingSample}&#8221;)<br \/>\nSomething1 has 2 Something2 and 3 Something3.<\/p>\n<p>What if you need to have a literal curly brace?<br \/>\n&gt;&gt;&gt; print(f&#8221;Hi {first}, this output needs a {{curly brace}}&#8221;)<br \/>\nHi Lisa, this output needs a {curly brace}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m accustomed to using variables directly in strings &#8212; most of my scripting experience was with Bash, Perl, and PHP. It&#8217;s something I have to think about when I would write Java or C++ code, and it&#8217;s one of the few things that struck me as a step backward in Python. This is basically the &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[664],"class_list":["post-5623","post","type-post","status-publish","format-standard","hentry","category-coding","tag-python"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5623"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5623\/revisions"}],"predecessor-version":[{"id":5624,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5623\/revisions\/5624"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}