{"id":56,"date":"2022-08-31T13:09:13","date_gmt":"2022-08-31T13:09:13","guid":{"rendered":"https:\/\/python.freelinuxtutorials.com\/?p=56"},"modified":"2022-08-31T13:10:21","modified_gmt":"2022-08-31T13:10:21","slug":"string-interpolation-and-formatting","status":"publish","type":"post","link":"https:\/\/python.freelinuxtutorials.com\/index.php\/2022\/08\/31\/string-interpolation-and-formatting\/","title":{"rendered":"Python>>String Interpolation and Formatting"},"content":{"rendered":"<p>String Interpolation is a process substituting values of variables into placeholders in a string<\/p>\n<p>Few methods as follow:<\/p>\n<p>a. .format() = is for simple positional formatting that\u00a0 uses to \u00a0substitute in place of braces {}.<\/p>\n<p>Example:<\/p>\n<blockquote><p>&gt;&gt; print (&#8216;Welcome to {}&#8217;.format(&#8216; Linux Tutorials&#8217;))<br \/>\nWelcome to Linux Tutorials<\/p>\n<p>&gt;&gt; title = &#8220;Linux Tutorials&#8221;<br \/>\n&gt;&gt;&gt; print(&#8216;Welcome to {}&#8217;.format(title))<br \/>\nWelcome to Linux Tutorials<\/p>\n<p>&gt;&gt; print (&#8216;Welcome to {} {} {}&#8217;.format (&#8216;Linux&#8217;, &#8216;Free&#8217;,&#8217;Tutorials&#8217;))<br \/>\nWelcome to Linux Free Tutorials<br \/>\n&gt;&gt;&gt; print (&#8216;Welcome to {1} {0} {2}&#8217;.format (&#8216;Linux&#8217;, &#8216;Free&#8217;,&#8217;Tutorials&#8217;))<br \/>\nWelcome to Free Linux Tutorials<\/p>\n<p>&gt;&gt; print(&#8216;Welcome to {1} {1}&#8217;.format(&#8216;Free&#8217;,&#8217;Linux&#8217;))<br \/>\nWelcome to Linux Linux<\/p>\n<p>&gt; print (&#8216;Welcome to {f} {l} {t}&#8217;.format(l=&#8217;linux&#8217;,f=&#8217;free&#8217;,t=&#8217;tutorials&#8217;))<br \/>\nWelcome to free linux tutorials<\/p>\n<p>&gt;&gt; quotient = 200\/46<br \/>\n&gt;&gt;&gt; quotient<br \/>\n4.3478260869565215<br \/>\n&gt;&gt;&gt; print(&#8216;The quotient is {q}&#8217;.format(q=quotient))<br \/>\nThe quotient is 4.3478260869565215<\/p><\/blockquote>\n<p>In float formatting, remember this: &#8220;<strong>{value:width.precision f}<\/strong>&#8221;<\/p>\n<blockquote><p>quotient<br \/>\n4.3478260869565215<br \/>\n&gt;&gt;&gt; print(&#8216;The quotient is {q:1.2f}&#8217;.format(q=quotient))<br \/>\nThe quotient is 4.35<br \/>\n&gt;&gt;&gt; print(&#8216;The quotient is {q:1.6f}&#8217;.format(q=quotient))<br \/>\nThe quotient is 4.347826<br \/>\n&gt;&gt;&gt; print(&#8216;The quotient is {q:10.1f}&#8217;.format(q=quotient))<br \/>\nThe quotient is\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 4.3<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>b. f-strings (formatted strings literal) (introduced in python 3.6+)<\/p>\n<blockquote><p>&gt;&gt; title =&#8217;Free Linux Tutorials&#8217;<br \/>\n&gt;&gt;&gt; title<br \/>\n&#8216;Free Linux Tutorials&#8217;<br \/>\n&gt;&gt;&gt; print (f&#8217;Welcome to {title}&#8217;)<br \/>\nWelcome to Free Linux Tutorials<\/p>\n<p>&gt;&gt;&gt; name = &#8216;Darwin&#8217;<br \/>\n&gt;&gt;&gt; look = &#8216;handsome&#8217;<br \/>\n&gt;&gt;&gt; print(f&#8217;The boy\\&#8217;s name is {name} and he is {look}&#8217;)<br \/>\nThe boy&#8217;s name is Darwin and he is handsome<\/p>\n<p>&gt;&gt; x = 88<br \/>\n&gt;&gt;&gt; y = 10<br \/>\n&gt;&gt;&gt; print (f&#8217;88 multiply by 10 is {x*y}&#8217;)<br \/>\n88 multiply by 10 is 880<\/p>\n<p>&gt;&gt; name = &#8216;Euan&#8217;<br \/>\n&gt;&gt;&gt; sname = &#8216;James&#8217;<br \/>\n&gt;&gt;&gt; age = &#8216;6&#8217;<br \/>\n&gt;&gt;&gt; print(f&#8217;My son\\&#8217;s name is {name} {sname} and he is {age} yrs old.&#8217;)<br \/>\nMy son&#8217;s name is Euan James and he is 6 yrs old.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>c. Template strings &#8211; import Template class from Python\u2019s built-in string module to use it.<\/p>\n<p>Example:<\/p>\n<blockquote><p>&gt; from string import Template<br \/>\n&gt;&gt;&gt; name = &#8216;Euan&#8217;<br \/>\n&gt;&gt;&gt; sname = &#8216;James&#8217;<br \/>\n&gt;&gt;&gt; full = Template(&#8216;Hello $name $sname!&#8217;)<br \/>\n&gt;&gt;&gt; print(full.substitute(name = name, sname = sname))<br \/>\nHello Euan James!<\/p><\/blockquote>\n<p>Old style Formatting:<\/p>\n<p>d. % formatting &#8211; is a unique built in operation that uses % operator to do string interpolation<\/p>\n<p>Example:<\/p>\n<blockquote><p>&gt;&gt; name = &#8216;Euan&#8217;<br \/>\n&gt;&gt;&gt; sname = &#8216;James&#8217;<br \/>\n&gt;&gt;&gt; print(&#8216;Hello %s %s !&#8217;%(name,sname))<br \/>\nHello Euan James !<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Take Note:<br \/>\n<span style=\"text-decoration: underline;\">Python String Formatting Rule of Thumb: <\/span><\/p>\n<p><em>If your format strings are user-supplied, use Template Strings to avoid security issues. Otherwise, use f-Strings if you\u2019re on Python 3.6+, and\u00a0 str.format if you\u2019re not.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>String Interpolation is a process substituting values of variables into placeholders in a string Few methods as follow: a. .format() = is for simple positional formatting that\u00a0 uses to \u00a0substitute in place of braces {}. Example: &gt;&gt; print (&#8216;Welcome to {}&#8217;.format(&#8216; Linux Tutorials&#8217;)) Welcome to Linux Tutorials &gt;&gt; title = &#8220;Linux Tutorials&#8221; &gt;&gt;&gt; print(&#8216;Welcome to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[1],"tags":[22,20,19,9,21],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-blog","tag-format","tag-fstring","tag-interpolation","tag-string","tag-template"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":9,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}