![]() |
|
[Tut] How to Print a String and an Integer - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Print a String and an Integer (/thread-99854.html) |
[Tut] How to Print a String and an Integer - xSicKxBot - 08-25-2022 How to Print a String and an Integer 5/5 – (1 vote) Problem Formulation and Solution Overview In this article, you’ll learn how to print a string and an integer together in Python. To make it more fun, we have the following running scenario: The Finxter Academy 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. First_Name | Puzzles Solved Steve | 39915 Amy | 31001 Peter | 29675 Marcus | 24150 Alice | 23580 Question: How would we write code to print a String and an Integer?We can accomplish this task by one of the following options:
Method 1: Use the print() function This example uses the Code: print()Code: print('Steve', 39915)This function offers the ability to accept various Data Types and output the results, separated by commas ( Code: ,Although not the most aesthetically pleasing output, it gets the job done. The Code: print()Steve 39915 Method 2: Use the print() function and str() method This example uses the Code: print()Code: str()Code: print('Steve has solved ' + str(39915) + ' puzzles!')To successfully output the contents of the Code: print()Code: str()Code: 39915Steve has solved 39915 puzzles! Method 3: Use f-string with print() function This example uses the Code: f-stringCode: print()Code: {}Code: first_name = 'Steve'
solved = 39915
print(f'{first_name} has solved {solved} puzzles to date!')Above, two (2) variables are declared: Code: first_nameCode: solvedThe Code: print()Code: {})Code: print()Steve has solved 39915 puzzles! What if you need to print out all Finxter users? This example assumes the data is saved to separate Code: ListsCode: ForCode: 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!')Steve has solved 39915 puzzles to date! Amy has solved 31001 puzzles to date! Peter has solved 29675 puzzles to date! Marcus has solved 24150 puzzles to date! Alice has solved 23580 puzzles to date! Method 4: Use %d, %s and %f Operator This examples uses the Code: %dCode: %sCode: %fCode: print()Code: 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 < len(f_name): print("%s solved %d puzzles with an average of %3.2f." % (f_name[i], f_solved[i], f_avg[i])) i += 1Above, three (3) Lists are declared. Each List carries different information for each user ( Code: f_nameCode: f_solvedCode: f_avgThe following line instantiates a Code: whileCode: f_nameInside the loop, the Code: %sCode: f_name[i]Code: %dCode: f_solved[i]Code: f_avg[i]Steve solved 39915 puzzles with an average of 99.31. Amy solved 31001 puzzles with an average of 82.68. Peter solved 29675 puzzles with an average of 79.56. Marcus solved 24150 puzzles with an average of 75.90. Alice solved 23580 puzzles with an average of 71.23. Note: In the Code: %3.2fMethod 5: Use identification numbers This example uses field identification numbers, such as 0, 1, 2, etc., inside the Code: print()Code: 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'))))Above, two (2) Code: ListsCode: ListCode: f_nameCode: f_solvedThen, using a Code: ForCode: ListsCode: {0}, {1}Code: format()Code: (format(f_solved[i], ',d')))Steve solved 39,915 puzzles! Amy solved 31,001 puzzles! Peter solved 29,675 puzzles! Marcus solved 24,150 puzzles! Alice solved 23,580 puzzles! Note: The data in Code: f_solvedCode: ',d'Method 6: Use f-string and a conditional This example uses an Code: f-stringCode: print()Code: 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}')Above, two (2) Lists are declared. Each Code: ListCode: f_nameCode: f_solvedInside the Code: print()Code: {}Has Alice solved more puzzles than Amy? False Bonus: Putting it Together! This article used several ways to format a String and an Integer. However, let’s put this together to generate a custom email body! The first step is to install the Pandas library. Click here for installation instructions. Code: 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())This code reads in a fictitious Code: finxter_top5.csv| First_Name | Last_Name | Solved | 0 | Steve | Hamilton | 39915 | steveh@acme.org 1 | Amy | Pullister | 31001 | amy.p@bminc.de 2 | Peter | Dunn | 29675 | pdunn@tsce.ca 3 | Marcus | Williams | 24150 | marwil@harpoprod.com 4 | Alice | Miller | 23580 | amiller@harvest.com Next, a For loop is instantiated to iterate through each row of the DataFrame Code: finxters Note: The underscore (Code: _Code: forFor each loop, the user’s email address is retrieved from the row position ( Code: row[3])Code: user_emailNext, the custom email body is formatted using the Code: f-stringCode: {row[0]} {row[1]}Code: solvedCode: ,Code: {row[2]:,d}Code: e_bodyFor this example, the first record displays. Hello Steve Hamilton, 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 A Finxter Challenge!Combine the knowledge you learned here to create a custom emailer. Click here for a tutorial to get you started! Summary These six (6) methods of printing Strings and Integers should give you enough information to select the best one for your coding requirements. Good Luck & Happy Coding! Programming Humor
|