1 of 16

2 of 16

Python Print

BASIC

3 of 16

  • oneThing can be any literal type1 5 "abc"
  • oneThing can be a variable of any type1 myVar
  • oneThing can be an expression that evaluates to any type1 x + 27 "rslt="+str(x)
  • oneThing can be a function that returns a value of any type1 str(yourVar) int(x)

print ONE thing

print(oneThing)

1 int, float, string, boolean

4 of 16

print ONE thing

print("Hello")

print(x)

print(sum/total)

print(type(myVar))

Hello

Hello

93.7

True

6.27

<class 'int'>

5 of 16

sum = 78.7

total = 84

print(str(sum) + "/" + str(total) + "=" + str(sum/total))

+ concatenates multiple strings into ONE string

Note: no spaces anywhere in output

print ONE thing (a string)

78.7/84=0.9369047

6 of 16

  • things are separated by commas
  • each thing can be any literal type1
  • each thing can be a variable of any type1
  • each thing can be an expression that evaluates to any type1
  • each thing can be a function that returns a value of any type1
  • when printed, the things are separated by a single space

print MULTIPLE things

print(thing1, thing2, thing3)

1 int, float, string, boolean

7 of 16

sum = 78.7

total = 84

things separated by commans print separated by a space

print MULTIPLE things

print(sum, "/",total, "=", sum/total)

78.7 / 84 = 93.7

8 of 16

By default once a print has printed all the specified thing(s) it will print a newline string. However, a different string can be specified using the end parameter

print("At the end", end = "") # print nothing instead of newline

print("At the end", end = " ") # print space instead of newline

print("Hello", "World", end="")

print("Hello", "LASA")

Hello WorldHello LASA

print "choose the end"

9 of 16

Python f-string

firstName = 'Grace'

lastName = 'Hopper'

fullName = f'{firstName} {lastName} LASA{{CS}}'

print(fullName)

Grace Hopper LASA{CS}

item = 'bananas'

price = 0.27

quantity = 6

print(f'{quantity} {item} cost ${price * quantity}')

6 bananas cost $1.62

The magic of f-strings is that you can embed Python expressions directly inside them. Any portion of an f-string that’s enclosed in curly braces { } is treated as an expression that is evaluated and converted to a string representation.

10 of 16

sum = 78.7

total = 84

print(f'{sum} / {total} = {sum/total}')

print(f'{sum}/{total}={sum/total}')

78.7 / 84 = 93.7

78.7/84=93.7

f-string example

11 of 16

advanced f-string

will not be on a quiz or test

12 of 16

Python f-strings

print(f'Price per item is ${price/quantity}')

Price per item is $0.28966666667

print(f'{price = }') # Python 3.8 introduced this self-documenting expression with the = character

price = 1.74

quantity = 6

item = 'bananas'

price = 0.27

print(f'{quantity:3d} {item} cost ${price*quantity:.1f}') # rounds too

6 bananas cost $1.6 rounds to 1 digit after decimal pt

print(f'Price per item is ${price*quantity:5.2f}') # rounds too

Price per item is $ 1.62 5 spaces, rounds to 2 digits after decimal pt

13 of 16

Python f-strings

Inside of { } put an

expression or variable optionally followed by

:[fill][alignment][width],.[decimals][type]

14 of 16

f-string examples

program is in speaker notes

15 of 16

f-string examples

program is in speaker notes

16 of 16

For more information