Download presentation
Presentation is loading. Please wait.
1
Formatting Output
2
Review We’ve run into a few problems in printing out our desired statements. Let’s look at a few of them: print(“$”, 3.99) >> $ # we don’t like that unnecessary space
3
Review We attempted to solve this problem by using concatenation with the math symbol “+” print(“$” ) # error! But this gave us an error, because we can’t add a string to an integer
4
Review So then, we used the conversion functions: int( ), float( ), str( ) which allowed us to convert data into the necessary data types print(“$” + str(3.99) ) >> $3.99 Note: The str( ) function converts data into a string.
5
Review Now, the only problem was that when we converted certain data into strings, we were no longer to use them in our math calculations. So, what else does Python have that might be able to help us?
6
Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments Example: print ( “one”) print ( “two”) >> one two
7
Line Endings Well now we can avoid this automatic space in between print statements by changing the ending of each print( ) function Example: print ( “one”, end = “” ) print ( “two”, end = “”) >> onetwo
8
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 (:
9
But why? This may not seem very helpful at the moment but later on, we will use this in our loops because it allow us to keep all of our printed data on one line as the user continues to input it
10
Separating Arguments print (“$”, 3.99) >> $ 3.99
By now, you should’ve also noticed that the print function prints a space in between each argument that is passed through it print (“$”, 3.99) >> $ 3.99
11
Separating Arguments print (“$”, 3.99, sep = “” ) >> $3.99
This can also be avoided as well by a separator specific command in the print function Example: print (“$”, 3.99, sep = “” ) >> $3.99
12
Separating Arguments print (“one”, “two”, sep = “” ) >> onetwo
Notice though, that you must use a comma to separate the given arguments and the final separator command Example: print (“one”, “two”, sep = “” ) >> onetwo
13
Separating Arguments You guessed it, you can also separate arguments however you’d like. More Examples: print (“hello world”, “!”, “?”, sep = “” ) >> hello world!? print (“one”, “two”, “three”, sep = “*” ) >> one*two*three
14
Combination You can also combine these commands into 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
15
Tab Command Another escape command is the tab command and it is denoted by “ \t ” This must be added inside a string A tab is the equivalent of 8 blank spaces
16
Tab Command Example: print (“First\t”, “Second\t”, “Third”) print (“10.9\t”, “11.2\t”, “11.4”) >> First Second Third
17
Tab Command Keep in mind, the escape commands are considered strings so you cannot add them to the end of a variable. You must create a new argument as a string. name = input(“What’s your name?”) print( name\t ) # this will cause an error
18
Tab Command Correct way name = input(“What’s your name?”) print( name, “\t”, “is the best!” ) >> What’s your name? Donald Donald is the best!
19
Reminder Remember that something to keep in mind: Python spaces each character the same, regardless of actual character width Example: “hello” “WQWQW” “I I I” Note: These all have the same string width in Python
20
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 % $ 79,435.60 Mercedes Benz 4.2% $ 119,324.54 Bentley % $234,674.93
21
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
22
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
23
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 Drake Rapping 101
24
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
25
Formatting a String We can achieve this task by adding extra spaces to either the beginning or the end of a string 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
26
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
27
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”, sep = “” ) print ( my_name , “Computer Programming”, sep = “” ) >> Name Class Donald Seok Computer Programming
28
Formatting a String You can also add a character in front of the instruction set to tell Python to add that character as many times as it needs in order to fulfill the total number of characters in the string The default is a blank space
29
Formatting a String x = format (“Name”, “>20s”) print (x) >> Name x = format (“Name”, “*>20s”) >> ****************Name
30
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
31
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) >>
32
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
33
Formatting Patterns a = / 6 print ( format( a, “.3f” ) ) # 3 digits after “.” >> print ( format( a, “,.3f” ) ) # 3 digits and commas >> 16, print ( format( a, “>20,.3f” ) ) # 3 digits, commas and 20 >> 16, characters, right justified
34
Formatting Numbers There’s a lot of these instruction sets.
Also, you may need to play around with the order of these instruction sets because the format of the characters is not always completely straight forward
35
Formatting Percentages
The “%” sign converts a number into a percentage by multiplying it by 100 and then adding the “%” symbol a = 0.52 print ( format( a, “ % ” ) ) >> % print ( format( a, “ .2% ” ) ) # percentage with 2 digits >> % after decimal point
36
Formatting Integer We can also add commas after each group of three digits The letter “d” is not necessary but denotes decimal notation print ( format( 20000, “ ,d ” ) ) >> 20,000 print ( format( , “ >20, ” ) ) >> ,000
37
Formatting Numbers The letter “b” converts a number into binary notation x = format(137, “b”) print(x) >>
38
Formatting Numbers This can be useful later on, but the letter “c” converts a number in decimal notation into it’s character from the UNICODE table print( format(68, “c”), format(69, “c”) , sep = “” ) >> DE
39
Formatting Numbers x = format(15647, “x”) print(x) >> 3d1f
For those of you interested in this kind of thing, the letter “x” actually converts a number into hexadecimal notation x = format(15647, “x”) print(x) >> 3d1f
40
Programming Challenge
Try this one again, but with the format( ) function Print an output like this: Car Name Interest Rate Price BMW % $ 79,435.60 Mercedes Benz 4.2% $ 119,324.54 Bentley % $234,674.93
41
Programming Challenge
Ask the user for four different numbers and then try getting an output like this: Decimal Binary UNICODE A B C D
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.