Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Print a String and an Integer

#1
How to Print a String and an Integer

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;593438&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn how to print a string and an integer together in Python.</p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">To make it more fun, we have the following running scenario:</p>
<p><em>The <a rel="noreferrer noopener" href="https://academy.finxter.com/" data-type="URL" data-id="https://academy.finxter.com/" target="_blank">Finxter Academy</a> has decided to send its users an encouraging message using their First Name (a String) and Problems Solved (an Integer). They have provided you with five (5) fictitious users to work with and to select the most appropriate option.</em></p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>First_Name</strong></td>
<td><strong><a href="https://app.finxter.com/learn/computer/science/" data-type="URL" data-id="https://app.finxter.com/learn/computer/science/" target="_blank" rel="noreferrer noopener">Puzzles</a> Solved</strong></td>
</tr>
<tr>
<td>Steve</td>
<td>39915</td>
</tr>
<tr>
<td>Amy</td>
<td>31001</td>
</tr>
<tr>
<td>Peter</td>
<td>29675</td>
</tr>
<tr>
<td>Marcus</td>
<td>24150</td>
</tr>
<tr>
<td>Alice</td>
<td>23580</td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio"/>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to print a String and an Integer?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function</li>
<li><strong>Method 2</strong>: Use the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-str-function/" data-type="URL" data-id="https://blog.finxter.com/python-str-function/" target="_blank"><code>str()</code></a> method</li>
<li><strong>Method 3</strong>: Use <code>f-string</code> with the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function</li>
<li><strong>Method 4</strong>: Use the <code>%d</code>, <code>%s</code> and <code>%f</code> operators</li>
<li><strong>Method 5</strong>: Use identification numbers</li>
<li><strong>Method 6</strong>: Use <code><a href="https://blog.finxter.com/string-formatting-vs-format-vs-formatted-string-literal/" data-type="post" data-id="13190" target="_blank" rel="noreferrer noopener">f-string</a></code> conditionals</li>
<li><strong>Bonus</strong>: Format CSV for output</li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 1: Use the print() function</h2>
<p class="has-global-color-8-background-color has-background">This example uses the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function to output a String and an Integer.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print('Steve', 39915)</pre>
<p>This function offers the ability to accept various Data Types and output the results, separated by commas (<code>,</code>) to the terminal.</p>
<p>Although not the most aesthetically pleasing output, it gets the job done. The <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function at its most simplistic level!</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Steve 39915</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-string-and-an-integer/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyXhEvg8Domk%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 2: Use the print() function and str() method</h2>
<p class="has-global-color-8-background-color has-background">This example uses the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function and the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-str-function/" data-type="URL" data-id="https://blog.finxter.com/python-str-function/" target="_blank"><code>str()</code></a> method to format and output a sentence containing a String and an Integer.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print('Steve has solved ' + str(39915) + ' puzzles!')</pre>
<p>To successfully output the contents of the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function, the Integer must first be converted to a String. This can be done by calling the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-str-function/" data-type="URL" data-id="https://blog.finxter.com/python-str-function/" target="_blank"><code>str()</code></a> method and passing, in this case, <code>39915</code> as an argument.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Steve has solved 39915 puzzles!</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-string-and-an-integer/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F4MdBRgQOkqc%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 3: Use f-string with print() function</h2>
<p class="has-global-color-8-background-color has-background">This example uses the <code><a href="https://blog.finxter.com/string-formatting-vs-format-vs-formatted-string-literal/" data-type="post" data-id="13190" target="_blank" rel="noreferrer noopener">f-string</a></code> inside the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function. This method uses curly brackets (<code>{}</code>) to accept and display the data.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">first_name = 'Steve'
solved = 39915
print(f'{first_name} has solved {solved} puzzles to date!')</pre>
<p>Above, two (2) variables are declared: <code>first_name</code> and <code>solved</code>. </p>
<p>The <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function is called and passed these two (2) variables, each inside curly braces (<code>{})</code>. This indicates that Python should expect two (2) variables of unknown Data Types. The <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function executes and sends this output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td> Steve has solved 39915 puzzles!</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-string-and-an-integer/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyWky-3tvyRY%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p>What if you need to print out all Finxter users? This example assumes the data is saved to separate <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>Lists</code></a> and output using a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>For</code></a> loop.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">f_name = ['Steve', 'Amy', 'Peter', 'Marcus', 'Alice']
f_solved = [39915, 31001, 29675, 24150, 23580] for i in range(len(f_name)): print(f'{f_name[i]} has solved {f_solved[i]} puzzles to date!')</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Steve has solved 39915 puzzles to date!<br />Amy has solved 31001 puzzles to date!<br />Peter has solved 29675 puzzles to date!<br />Marcus has solved 24150 puzzles to date!<br />Alice has solved 23580 puzzles to date!</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-string-and-an-integer/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fjk6JTGAmXJE%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 4: Use %d, %s and %f Operator</h2>
<p class="has-global-color-8-background-color has-background">This examples uses the <code>%d</code> (decimal value), the <code>%s</code> (string value), and <code>%f</code> (float value) inside the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function to output the fictitious Finxter user’s data.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="5-8" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">f_name = ['Steve', 'Amy', 'Peter', 'Marcus', 'Alice']
f_solved = [39915, 31001, 29675, 24150, 23580]
f_avg = [99.315, 82.678, 79.563, 75.899, 71.233] i = 0
while i &lt; len(f_name): print("%s solved %d puzzles with an average of %3.2f." % (f_name[i], f_solved[i], f_avg[i])) i += 1</pre>
<p>Above, three (3) <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">Lists</a> are declared. Each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> carries different information for each user (<code>f_name</code>, <code>f_solved</code>, <code>f_avg</code>).</p>
<p>The following line instantiates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>while</code></a> loop and a counter (i) which increments upon each iteration. This loop iterates until the final element in <code>f_name</code> is reached.</p>
<p>Inside the loop, the <code>%s</code> (accepts strings) is replaced with the value of <code>f_name[i]</code>. Then, <code>%d</code> (accepts integers) is replaced with the value of <code>f_solved[i]</code>. Finally, the %3.2f (for floats) value of is replaced with <code>f_avg[i]</code> having two (2) decimal places. The output displays below.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Steve solved 39915 puzzles with an average of 99.31.<br />Amy solved 31001 puzzles with an average of 82.68.<br />Peter solved 29675 puzzles with an average of 79.56.<br />Marcus solved 24150 puzzles with an average of 75.90.<br />Alice solved 23580 puzzles with an average of 71.23.</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: In the <code>%3.2f</code> annotation, the value of three (3) indicates the width, and 2 indicates the number of decimal places. Try different widths! </p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-print-a-string-and-an-integer/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqXOFngx0bQU%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 5: Use identification numbers</h2>
<p class="has-global-color-8-background-color has-background">This example uses field identification numbers, such as 0, 1, 2, etc., inside the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function to identify the fields to display and in what order.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">f_name = ['Steve', 'Amy', 'Peter', 'Marcus', 'Alice']
f_solved = [39915, 31001, 29675, 24150, 23580] for i in range(len(f_name)): print('{0} solved {1} puzzles!'.format(f_name[i], (format(f_solved[i], ',d'))))</pre>
<p>Above, two (2) <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>Lists</code></a> are declared. Each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> carries different information for each Finxter user (<code>f_name</code>, <code>f_solved</code>). </p>
<p>Then, using a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>For</code></a> loop, the code runs through the above <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank"><code>Lists</code></a>. The numbers wrapped inside curly braces (<code>{0}, {1}</code>) indicate holding places for the expected data. This data appears inside the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-format-function/" data-type="URL" data-id="https://blog.finxter.com/python-format-function/" target="_blank"><code>format()</code></a> function (<code>(format(f_solved[i], ',d')))</code>) and are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Steve solved 39,915 puzzles!<br />Amy solved 31,001 puzzles!<br />Peter solved 29,675 puzzles!<br />Marcus solved 24,150 puzzles!<br />Alice solved 23,580 puzzles!</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />Note: The data in <code>f_solved</code> is formatted to display a thousand comma (<code>',d'</code>).</p>
<h2>Method 6: Use f-string and a conditional</h2>
<p class="has-global-color-8-background-color has-background">This example uses an <code>f-string</code> and a conditional to display the results based on a condition inside the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">f_name = ['Steve', 'Amy', 'Peter', 'Marcus', 'Alice']
f_solved = [39915, 31001, 29675, 24150, 23580]
print(f'Has Alice solved more puzzles than Amy? {True if f_solved[4] > f_solved[1] else False}')</pre>
<p>Above, two (2) <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">Lists</a> are declared. Each <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank"><code>List</code></a> carries different information for each Finxter user (<code>f_name</code>, <code>f_solved</code>).</p>
<p>Inside the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="URL" data-id="https://blog.finxter.com/python-print/" target="_blank"><code>print()</code></a> function, the code inside the curly braces (<code>{}</code>) checks to see if the number of puzzles Alice has solved is greater than the number of puzzles Amy has solved. True or False returns based on the outcome and is output along with the String to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Has Alice solved more puzzles than Amy? False</td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2 class="has-base-3-color has-text-color has-background" style="background-color:#9f0707">Bonus: Putting it Together!</h2>
<p class="has-global-color-8-background-color has-background">This article used several ways to format a String and an Integer. However, let’s put this together to generate a custom email body! </p>
<p>The first step is to install the Pandas library. Click <a href="https://blog.finxter.com/how-to-install-pandas-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-pandas-in-python/" target="_blank" rel="noreferrer noopener">here</a> for installation instructions.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd finxters = pd.read_csv('finxter_top5.csv') for _, row in finxters.iterrows(): user_email = row[3] e_body = f""" Hello {row[0]} {row[1]},\n The Finxter Academy wants to congratulate you on solving {row[2]:,d} puzzles. For achieving this, our Team is sending you a free copy of our latest book! Thank you for joining us. The Finxter Academy """ print(e_body.strip())</pre>
<p>This code <a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" data-type="post" data-id="440655" target="_blank" rel="noreferrer noopener">reads</a> in a fictitious <code>finxter_top5.csv</code> file.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td></td>
<td>First_Name</td>
<td>Last_Name</td>
<td>Solved</td>
<td>Email</td>
</tr>
<tr>
<td>0</td>
<td>Steve</td>
<td>Hamilton</td>
<td>39915</td>
<td>[email protected]</td>
</tr>
<tr>
<td>1</td>
<td>Amy</td>
<td>Pullister</td>
<td>31001</td>
<td>[email protected]</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Dunn</td>
<td>29675</td>
<td>[email protected]</td>
</tr>
<tr>
<td>3</td>
<td>Marcus</td>
<td>Williams</td>
<td>24150</td>
<td>[email protected]</td>
</tr>
<tr>
<td>4</td>
<td>Alice</td>
<td>Miller</td>
<td>23580</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
</figure>
<p>Next, a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank">For</a> loop is instantiated to iterate through each row of the DataFrame <code>finxters</code>.</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: The underscore (<code>_</code>) character in the <code>for</code> loop indicates that the value is unimportant and not used, but needed.</p>
<p>For each loop, the user’s email address is retrieved from the row position (<code>row[3])</code>. This email address saves to <code>user_email</code>.</p>
<p>Next, the custom email body is formatted using the <code>f-string</code> and passed the user’s First Name and Last Name in the salutation (<code>{row[0]} {row[1]}</code>). Then, the <code>solved</code> variable is formatted to display commas (<code>,</code>) indicating thousands (<code>{row[2]:,d}</code>). The results are saved to <code>e_body</code> and, for this example, are output to the terminal.</p>
<p>For this example, the first record displays.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Hello Steve Hamilton,<br />The Finxter Academy wants to congratulate you on solving 39,915 puzzles. For achieving this, our Team is sending you a free copy of our latest book. Thank you for joining us. The Finxter Academy</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-text-align-center has-base-3-color has-text-color has-background" style="background-color:#9f0707"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f9e9.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />A Finxter Challenge!<br />Combine the knowledge you learned here to create a custom emailer.<br />Click <a rel="noreferrer noopener" href="https://blog.finxter.com/email-a-pdf-in-python/" data-type="URL" data-id="https://blog.finxter.com/email-a-pdf-in-python/" target="_blank">here</a> for a tutorial to get you started!</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio" id="block-323ff1c3-82fc-4193-9c8d-125f5d3c2efb">Summary</h2>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio" id="block-ebf714b3-b2f5-494e-bcd5-0cd3b3709ed7">These six (6) methods of printing Strings and Integers should give you enough information to select the best one for your coding requirements.</p>
<p>Good Luck &amp; Happy Coding!</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Programming Humor</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="648" height="525" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-119.png" alt="" class="wp-image-528686" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-119.png 648w, https://blog.finxter.com/wp-content/uplo...00x243.png 300w" sizes="(max-width: 648px) 100vw, 648px" /></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/08/...n-integer/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016