Download presentation
Presentation is loading. Please wait.
1
Let’s get some practice!
Formatting Output Let’s get some practice!
2
Formatting a String x = format (“Name”, “>20s”) print (x) >> Name x = format (“Name”, “*>20s”) >> ****************Name
3
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
4
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
5
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
6
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
7
Formatting Numbers The letter “b” converts a number into binary notation x = format(137, “b”) print(x) >>
8
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
9
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
10
Programming Challenge
Write a program that asks the user for three different items they would like to buy. Ask them for the price of each item and output something that looks like this: Item Price Bananas > $ Milk > $ Bread > $
11
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.