Python · basics

Input & Output

Show results on the screen with print(), and control exactly what it writes.

Basic print


          # simple print
          print("Hello, World!")
          # ans: Hello, World!
          print("Python")
          # ans: Python
        

The ↵ marks the newline print adds at the end of every call.

Print multiple values


          # multiple arguments
          print("Age:", 25)
          # ans: Age: 25
          print("Name:", "Alice", "Score:", 95)
          # ans: Name: Alice Score: 95
        

Separate arguments are joined with a space by default.

Print separator


          # custom separator
          print("a", "b", "c", sep="-")
          # ans: a-b-c
          print(1, 2, 3, sep=" | ")
          # ans: 1 | 2 | 3
        
  1. a-b-c↵sep between the values, end after them
  2. a-b-c
  1. 1␣|␣2␣|␣3↵␣ is a space
  2. 1 | 2 | 3

Print end character


          # custom end character
          print("Hello", end="")
          print("World")
          # ans: HelloWorld
          print("Line1", end=" ")
          print("Line2")
          # ans: Line1 Line2
        

end replaces the newline, so the next print continues on the same line.

Print expressions


          # print with expression
          print(5 + 3)
          # ans: 8
          x = 10
          print("x =", x)
          # ans: x = 10
        

Python works out the expression first, then prints the result.

Exercises

Say exactly what appears on screen, then press →.

Exercises · part 1


          # what is printed?
          print(1, 2, 3)
          # ans: 1 2 3
          # what separator is used?
          print("a", "b", "c", sep="-")
          # ans: a-b-c
        

Exercises · part 2


          # what is printed?
          print("Hello", end="")
          print("World")
          # ans: HelloWorld
          # multiple arguments?
          print(5 + 3)
          # ans: 8
        

Exercises · part 3


          # what does this print?
          print("Result:", 10, 20, sep=", ")
          # ans: Result:, 10, 20
          # what about this?
          print()
          # ans: (empty line)
        

Exercises · part 4


          # print with expression?
          x = 5
          print("x =", x)
          # ans: x = 5
          # what is the output?
          print("Line1\nLine2")
          # ans: Line1
          # ans: Line2 (on separate lines)
        

Exercises · part 5


          # tab character?
          print("Name:\tAlice")
          # ans: Name:    Alice (with tab)
          # escape quotes?
          print("He said \"Hi\"")
          # ans: He said "Hi"
        

Exercises · part 6


          # raw string?
          print(r"C:\new\path")
          # ans: C:\new\path (backslashes not escaped)
          # print return value?
          x = print("Hello")
          # ans: Hello (printed)
          # ans: x is None
        

Exercises · part 7


          # empty print?
          print("")
          # ans: (empty line)
          # multiple newlines?
          print("A\n\nB")
          # ans: A
          # ans: (blank line)
          # ans: B
        

Remember

sep

Goes between the values; a space by default.

end

Goes after the last value; a newline by default.

Escapes

\n is a newline, \t a tab, \" a quote; an r"..." string keeps backslashes as they are.

print returns None

It shows text; it does not give a value back.