Download presentation
Presentation is loading. Please wait.
1
Formatting Output
2
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
3
The Inefficiency of the TAB Command
The TAB command is inefficient and you should NEVER, or almost never use it to format in Python Because …
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
Formatting a String x = format (“Name”, “>20s”) print (x) >> Name x = format (“Name”, “*>20s”) >> ****************Name
13
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
14
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) >>
15
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
16
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
17
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
18
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
19
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
20
Formatting Numbers The letter “b” converts a number into binary notation x = format(137, “b”) print(x) >>
21
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
22
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
23
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
24
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.