Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formatting Output.

Similar presentations


Presentation on theme: "Formatting Output."— Presentation transcript:

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


Download ppt "Formatting Output."

Similar presentations


Ads by Google