# Noemi Cabrera
# 3 December 2021
# In this lesson, I learned how to format output using the 'old' and 'python' style including numbers,
# strings, lists, and floats. There are many flags and key letters you need to use in order to format
# your output the way you desire. For example, for a string you use s, for an integer you use d, and for a
# float you use f. Both of these styles are similar, but the 'old' style is much more limited.
# Also, I learned padding and alignment for each of these styles.
# I didn't have any major difficulties. A few times, I just forgot to count the characters of the string/number itself
# plus the spaces when I needed to add spaces or certain characters to a string/integer.
# In this code, modulo (%) is used to format the year and price variables.
# %d signs the integer in 'year' as a string and %f signs the float value in 'price' as a string
# in the print statement.
year = 2017
price = 2.42011
print('The national average gas price in %d was $ %f' % (year, price))
# In this code, modulo (%) is used to format the year and price variables.
# %i signs the integer in 'year' as a string and %3.2 signs the float value in 'price' as a string
# displaying 3 digits and 2 decimal places in the print statement.
year = 2017
price = 2.42011
print('The national average gas price in %i was $ %3.2f' % (year, price))
# In this code, modulo (%) is used to format the year and price variables.
# %i signs the integer in 'year' as a string and %08.2F signs the float value in 'price' as a string
# displaying 8 digits and 2 decimal places in the print statement. Since there are not enough characters
# zeros are added.
year = 2017
price = 2.42011
print('The national average gas price in %i was $ %08.2F' % (year, price))
# In this code, modulo (%) is used to format the year and price variables.
# %d signs the integer in 'year' as a string and %e signs the float value in 'price' as an
# exponential number with 1 digit to the left of the decimal point. The rest is expressed with an exponent.
year = 2017
price = 2.42011
print('The national average gas price in %d was $ %e' % (year, price))
# In this code, modulo (%) is used to format the year and price variables.
# %d signs the integer in 'year' as a string and %3.2e signs the float value in 'price' as an
# exponential number with 1 digit to the left of the decimal point. The rest is expressed with an exponent.
# 3.2 specifies that there are 3 digitis and 2 of those have to be in the decimal place.
year = 2017
price = 2.42011
print('The national average gas price in %d was $ %3.2e' % (year, price))
# In this code, modulo (%) is used to format the year and price variables.
# %d signs the integer in 'year' as a string and %3.2E signs the float value in 'price' as an
# exponential number with 1 digit to the left of the decimal point. The rest is expressed with an exponent.
# 3.2 specifies that there are 3 digitis and 2 of those have to be in the decimal place.
year = 2017
price = 2.42011
print('The national average gas price in %d was $ %3.2E' % (year, price))
# The user is asked to enter their first and last name and then modulo (%) is used to format the
# desired output. %c contains the first character of the 'first_name' variable. %s contains the last 2
# characters of the last_name variable. The first letter of the first name and the last 2 letters of
# the last name are displayed.
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
print('The first name starts with: %c' % (first_name[0]))
print('The last name ends with the 2 chars: %s' %(last_name[-2:]))
# In this code, the pi fucntion is imported from the math module. Then, the modulo(%) is used to display
# the value of pi with 18 characters before the decimal point and 2 after it by using %20.2f.
# Also, the value of pi is displayed with 1 character before the decimal point and 20 after it by using %-20.2f.
from math import pi
print("Right adjusted: %20.2f" %(pi))
print("Left adjusted: %-20.2f" %(pi))
# In this code, modulo(%) is used to format the variables F & C. The %d represents the signed integer, whic in this case is
# F and then %f represents the signed float number, which in this case is C. The value of F stays the same,
# but the value of C is formated in many different ways to achieve the desired outcomes.
# [ ] Use the variables `F` and `C` to generate the following print outputs
'''
75 Fahrenheit = 23.888900 Celsius
75 Fahrenheit = 23.89 Celsius
75 Fahrenheit = 000023.889 Celsius
75 Fahrenheit = 23.889 Celsius
75 Fahrenheit = 23.889 Celsius
75 Fahrenheit = 2.389E+01 Celsius
75 Fahrenheit = 2.389e+01 Celsius
'''
F = 75
C = 23.8889
print("%d Fahrenheit = %09f Celsius" %(F, C))
print("%d Fahrenheit = %4.2f Celsius" %(F, C))
print("%d Fahrenheit = %09.3f Celsius" %(F, C))
print("%d Fahrenheit = %-10.3f Celsius" %(F, C))
print("%d Fahrenheit = %10.3f Celsius" %(F, C))
print("%d Fahrenheit = %1.3e Celsius" %(F, C))
print("%d Fahrenheit = %1.3E Celsius" %(F, C))
# In this code, modulo(%) is used to format the variables first_name & last_name. The %s represents the signed string, which in this case is
# in first_name, and then the second %s represents the signed string, which in this case is in last_name.Both vairables are formated
# in many different ways to achieve the desired outcomes. They can only be added spaces with the 'old' method.
# [ ] Use the string variables below to generate the following print outputs
# HINT: Set the name formatter width to 10 characters
first_name = 'Tamara'
last_name = 'Babic'
'''
Name: Tamara Babic
Name: Tamara Babic
Name: Tamara Babic
Name: Tamara Babic
Name: Tamara Babic
Name: Tamara Babic
'''
print("Name: %s %s"%(first_name, last_name))
print("Name: %10s %10s"%(first_name, last_name))
print("Name: %s %14s"%(first_name, last_name))
print("Name: %10s %s"%(first_name, last_name))
print("Name: %s %9s"%(first_name, last_name))
print("Name: %6s %5s"%(first_name, last_name))
# In this code, the 'data' list is formatted so that the items display as a table containing name, ID, and email.
# The title of each column is displayed using print(). Then, a for loop is used to iterate through each item/list
# in the data list. Each item withing the lists of data is formatted using modulo (%). The first
# string item is set to 21 characters (spaces are added to the right if not enough characters). The second integer
# item is set to 11 characters (spaces are added to the right if not enough characters). Lastly, the
# last string is displayed as it is.
# [ ] The list `data` contains personnel information (Name, ID, email)
# Write a program to print out the data as follows:
'''
Name ID Email
--------------------------------------------------
Suresh Datta 57394 suresh@example.com
Colette Browning 48539 colette@example.com
Skye Homsi 58302 skye@example.com
Hiroto Yamaguchi 48502 hiroto@example.com
Tobias Ledford 48291 tobias@example.com
Jin Xu 48293 jin@example.com
Joana Dias 23945 joana@example.com
Alton Derosa 85823 alton@example.com
'''
data = [["Suresh Datta", 57394, "suresh@example.com"], ["Colette Browning", 48539, "colette@example.com"], ["Skye Homsi", 58302, "skye@example.com"], ["Hiroto Yamaguchi", 48502, "hiroto@example.com"], ["Tobias Ledford", 48291, "tobias@example.com", "Tamara Babic", 58201, "tamara@example.com"], ["Jin Xu", 48293, "jin@example.com"], ["Joana Dias", 23945, "joana@example.com"], ["Alton Derosa", 85823, "alton@example.com"]]
print("Name ID Email")
print("----------------------------------------------------")
for item in data:
print("%-21s %-11d %s"%(item[0],item[1], item[2]))
# In this code, .format() is used to format the year and price variables.
# {:d} signs the integer in 'year' as a string and {:f} signs the float value in 'price' as a string
# in the print statement.
year = 2017
price = 2.42011
print('The national average gas price in {:d} was $ {:f}'.format(year, price))
# In this code, .format() is used to format the year and price variables.
# {0:d} signs the integer in 'year' as a string and {1:3.2F} signs the float value in 'price'
# with just 3 digits, 2 of those being after the decimal point.
year = 2017
price = 2.42011
print('The national average gas price in {0:d} was $ {1:3.2F}'.format(year, price))
# In this code, .format() is used to format the year and price variables. Each variable is assigned to another variable
# so that you can specify which one you're using and where in the print statement.
# {y:d} signs the integer in 'year' as a string and {p:1.2e} signs the float value in 'price'
# as an exponent number displaying 2 decimal places.
year = 2017
price = 2.42011
print('The national average gas price in {y:d} was $ {p:1.2e}'.format(y = year, p = price))
# In this code, .format() is used to format the year and price variables.
# {:d} signs the integer in 'year' as a string and {:010.2f} signs the float value in 'price'
# with 10 characters (0 will be used to replace the spaces of the characters needed to reach 10).
# The number will show 2 decimal places.
year = 2017
price = 2.42011
print('The national average gas price in {:d} was $ {:010.2f}'.format(year, price))
# The user is asked to enter their first and last name and then .format() is used to format the
# desired output. {:s} contains the first character of the 'first_name' variable. The next {:s} contains
# the last 2 characters of the last_name variable. The first letter of the first name and the last 2 letters of
# the last name are displayed.
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
print('The first name starts with: {:s}'.format(first_name[0]))
print('The last name ends with the 2 chars: {:s}'.format(last_name[-2:]))
# In this code, the pi function is imported from the math module. Then, .format is used to format the
# value of pi. {:<20.2f} adds spaces to the right to reach a total of 20 characters.
# {:^20.2f} adds spaces and places the value of pi in the center to reach a total of 20 characters.
# {:>20.2f} adds spaces to the left to reach a total of 20 characters.
# Pi has only two decimal places.
from math import pi
print('Right adjusted : {:<20.2f}'.format(pi))
print('Center adjusted: {:^20.2f}'.format(pi))
print('Left adjusted : {:>20.2f}'.format(pi))
# In this code, the pi function is imported from the math module. Then, .format is used to format the
# value of pi. {:<20.2f} adds zeros to the right to reach a total of 20 characters.
# {:^20.2f} adds zeros and places the value of pi in the center to reach a total of 20 characters.
# {:>20.2f} adds zeros to the left to reach a total of 20 characters.
# Pi has only two decimal places.
from math import pi
# Padding with zeros
print('Right adjusted : {:0<20.2f}'.format(pi))
print('Center adjusted: {:0^20.2f}'.format(pi))
print('Left adjusted : {:0>20.2f}'.format(pi))
# In this code, the pi function is imported from the math module. Then, .format is used to format the
# value of pi. {:<20.2f} adds spaces to the right to reach a total of 20 characters.
# {:^20.2f} adds underscores and underscores the value of pi in the center to reach a total of 20 characters.
# {:>20.2f} adds underscores to the left to reach a total of 20 characters.
# Pi has only two decimal places.
from math import pi
# Padding with underscore characters
print('Right adjusted : {:_<20.2f}'.format(pi))
print('Center adjusted: {:_^20.2f}'.format(pi))
print('Left adjusted : {:_>20.2f}'.format(pi))
# In this code, .format() is used to format the variables F & C. The {:d} represents a signed integer, which in this case is
# F and then{:0<9f} represents the signed float number, which in this case is C. The value of F stays the same,
# but the value of C is formated in many different ways to achieve the desired outcomes.
# With this style, the float can be added zeros not only spaces.
# [ ] Use Python-style formatting and the variables `F` and `C` to generate the following print outputs
'''
75 Fahrenheit = 23.888900 Celsius
75 Fahrenheit = 23.89 Celsius
75 Fahrenheit = 0000023.89 Celsius
75 Fahrenheit = 23.889 Celsius
75 Fahrenheit = 23.889 Celsius
75 Fahrenheit = 23.889 Celsius
75 Fahrenheit = 2.389E+01 Celsius
'''
F = 75
C = 23.8889
print("{:d} Fahrenheit = {:0<9f} Celsius".format(F, C))
print("{:d} Fahrenheit = {:4.2f} Celsius".format(F, C))
print("{:d} Fahrenheit = {:0>10.2f} Celsius".format(F, C))
print("{:d} Fahrenheit = {:<10.3f} Celsius".format(F, C))
print("{:d} Fahrenheit = {:^10.3f} Celsius".format(F, C))
print("{:d} Fahrenheit = {:>10.3f} Celsius".format(F, C))
print("{:d} Fahrenheit = {:5.3E} Celsius".format(F, C))
# In this code, .format() is used to format the variables first_name & last_name. The {:s} represents the signed string, which in this case is
# in first_name, and then the second {:s} represents the signed string, which in this case is in last_name. Both vairables are formated
# in many different ways to achieve the desired outcomes. With this style, they can be also added underscores, not only spaces like in the 'old' method.
# [ ] Use Python-style formatting and the string variables below to generate the following print outputs
# HINT: Set the name formatter width to 10 characters
first_name = 'Tamara'
last_name = 'Babic'
'''
Name: Tamara Babic
Name: Tamara Babic
Name: Tamara____ _____Babic
Name: __Tamara__ __Babic___
Name: ____Tamara Babic_____
Name: Tamara Babic
'''
print("Name: {:s} {:s}".format(first_name, last_name))
print("Name: {:s} {:>14s}".format(first_name, last_name))
print("Name: {:_<10s} {:_>10s}".format(first_name, last_name))
print("Name: {:_^10s} {:_^10s}".format(first_name, last_name))
print("Name: {:_>10s} {:_<10s}".format(first_name, last_name))
print("Name: {:>10s} {:<10s}".format(first_name, last_name))
# In this code, the 'data' list is formatted so that the items display as a table containing name, ID, and email.
# The title of each column is displayed centered in each column by using .format() in a print statement. Then,
# a for loop is used to iterate through each item/list in the data list. Each item within the lists of 'data' is
# formatted using .format(). The first string item is set to 21 characters (item is centered in the column by using ^).
# The second integer item is set to 12 characters (item is centered in the column by using ^). Lastly, the
# last string is set to 21 characters (item is aligned at the right in the column by using >).
# [ ] The list `data` contains personnel information (Name, ID, email)
# Write a program using Python-style formatting to print out the data as follows:
'''
Name | ID | Email
________________________________________________________
Suresh Datta | 57394 | suresh@example.com
Colette Browning | 48539 | colette@example.com
Skye Homsi | 58302 | skye@example.com
Hiroto Yamaguchi | 48502 | hiroto@example.com
Tobias Ledford | 48291 | tobias@example.com
Jin Xu | 48293 | jin@example.com
Joana Dias | 23945 | joana@example.com
Alton Derosa | 85823 | alton@example.com
'''
data = [["Suresh Datta", 57394, "suresh@example.com"], ["Colette Browning", 48539, "colette@example.com"], ["Skye Homsi", 58302, "skye@example.com"], ["Hiroto Yamaguchi", 48502, "hiroto@example.com"], ["Tobias Ledford", 48291, "tobias@example.com"], ["Tamara Babic", 58201, "tamara@example.com"], ["Jin Xu", 48293, "jin@example.com"], ["Joana Dias", 23945, "joana@example.com"], ["Alton Derosa", 85823, "alton@example.com"]]
n= "Name"
ID = "ID"
e = "Email"
print("{:^21s}|{:^12s}|{:^21s}".format(n, ID, e))
print("--------------------------------------------------------")
for item in data:
print("{:^21s}|{:^12d}|{:>21s}".format(item[0],item[1], item[2]))