Formatting Output
Line Endings• By now, you’ve noticed that the print( ) function will
automatically print out a new line after passing all of the arguments
• However, you can avoid this by changing the end behavior inside the print function
Example:
print ( “one”, end = “” )
print ( “two”, end = “” )
>> onetwo
Line Endings• It turns out, you can actually ask the print
function to add whatever you’d like at the end of it’s execution
print (“one”, end = “***” )
print (“two”, end = “ ” )
print (“three”, end = “ (: ” )
>> one***two three (:
Separating Arguments •By now, you should’ve also noticed that the
print function prints a space in between each argument that is passed through it
• This can also be avoided as well by a “sep” command in the print function
Example:
print (“one”, “two”, sep = “” )
>> onetwo
Separating Arguments More Examples:
print (“hello world”, “!”, “?”, sep = “” )
>> hello world!?
print (“one”, “two”, “three”, sep = “*” )
>> one*two*three
Combination • You can combine these commands in a single
print function
Example:
print (“a”, “b”, “c”, sep = “**”, end = “$” )
print (“d”, “e”, “f”, sep = “-”, end = “” )
>> a**b**c$d-e-f
• The order does not matter
Tab Command • Another escape command is the tab command and
it is denoted by “ \t ”
• This must be added inside a string
Example:
print (“First”, “\t”, “Second”, “\t”, “Third”)
print (“10.9”, “\t”, “11.2”, “\t”, “11.4”)
>> First Second Third
10.9 11.2 11.4
String Concatenation • You cannot “add” strings together but we can
concatenate them with the addition operator
print (“Donald” + “Seok”)
>> DonaldSeok
print (“19” + “20”)
>> 1920
String Repetition • You can also “multiply” strings to print them
out repeatedly
Example:
lyrics = “Fa ” + “La ” * 8
Print (lyrics)
>> Fa La La La La La La La La
Formatting a String • Python also has a format( ) command
• This command allows you to format a string and returns it as a new piece of data
• This can be done in a variable or directly in the print function
Formatting a String • The format( ) function accepts two arguments
• The first argument is the piece of data you want to format (we will work with strings first)
• The second argument is the formatting pattern you would like it to follow
Formatting a String •One common pattern of formatting is to
ensure that a string has a known number of characters
• For example, let’s say you want your output to look like this:
Name Class
Donald Seok Computer Programming
Formatting a String Name Class
Donald Seok Computer Programming
• You’ll need the strings “Name” and “Donald Seok” to have the same number of characters in them so that the strings “Class” and “Computer Programming” will align perfectly after them
Formatting a String • Something to keep in mind: Python spaces
each character the same, regardless of actual character width
Example:
“hello”
“WQWQW”
“I I I”
- These all have the same string width in Python
Formatting a String •We can achieve this task by adding extra spaces to
either the beginning or the end of a string
Example:
x = format (“Name”, “<20s”)
• This generates a string with 20 characters, which means Python will add 16 spaces after the 4 characters in the word “Name”
• The “<“ character means left justify the string and place extra spaces at the “end” of the new string
Formatting a String • You guessed it … you can also tell Python to
right justify the string and add spaces to the beginning of the string
Example:
x = format (“Name”, “>20s”)
print (x)
>> Name
^ 16 blank characters
Formatting a String • So, let’s try making that output:
word_name = format (“Name”, “<20s”)
my_name = format(“Donald Seok”, “<20s”)
print ( word_name , “Class”)
print ( my_name , “Computer Programming”)
>> Name Class
Donald Seok Computer Programming
Formatting Numbers
•The format( ) command also works on numbers
•However, it is important to keep in mind that the number, whether integer or float, will be returned as a string from the function
Formatting Numbers
•This command would’ve come in handy when we were writing programs that printed out prices
•This is what we’re used to seeing:
a = 1 / 3
Print (a)
>> 0.3333333333333333
Formatting Numbers
•Now, using the format function:
a = 1 / 3
b = format ( a , “.2f” )
print (b)
>> 0.33
#The number denotes the number of characters you would like to remain after the decimal point
Formatting Patterns a = 100000 / 6
print ( format( a, “.3f” ) ) # 3 digits after “.”
>> 16666.666
print ( format( a, “,.3f” ) ) # 3 digits and commas
>> 16,666.666
print ( format( a, “>20,.3f” ) ) # 3 digits, commas and 20
>> 16,666.666characters, right justified
Formatting Percentages a = 0.52
print ( format( a, “ % ” ) ) # convert to percentage
>> 52.000000 % multiply by 100
print ( format( a, “ .2% ” ) ) # percentage with 2 digits
>> 52.00 %after decimal point
print ( format( a, “ .0% ” ) ) # percentage as integer
>> 52 %
Formatting Integer a = 20000
print ( format( a, “ ,d ” ) ) # add commas
>> 20,000
print ( format( a, “ >20, ” ) ) # add commas and 20
>> 20,000 characters, right justified
Programming Challenge
•Write a program that asks the user for three cars, three different interest rates, and three prices
•Print an output like this:
Car Name Interest Rate Price
BMW 3.0% $ 79,435.60
Mercedes Benz 4.2% $ 119,324.54
Bentley 6.5% $234,674.93