Input & Output

Basic Print

  1. # simple print
  2. print("Hello, World!")
  3. #ans: Hello, World!
  4. print("Python")
  5. #ans: Python
  1. # multiple arguments
  2. print("Age:", 25)
  3. #ans: Age: 25
  4. print("Name:", "Alice", "Score:", 95)
  5. #ans: Name: Alice Score: 95
  1. # custom separator
  2. print("a", "b", "c", sep="-")
  3. #ans: a-b-c
  4. print(1, 2, 3, sep=" | ")
  5. #ans: 1 | 2 | 3
  1. # custom end character
  2. print("Hello", end="")
  3. print("World")
  4. #ans: HelloWorld
  5. print("Line1", end=" ")
  6. print("Line2")
  7. #ans: Line1 Line2
  1. # print with expression
  2. print(5 + 3)
  3. #ans: 8
  4. x = 10
  5. print("x =", x)
  6. #ans: x = 10

Exercises - Part 1

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

Exercises - Part 2

  1. # what is printed?
  2. print("Hello", end="")
  3. print("World")
  4. #ans: HelloWorld
  5. # multiple arguments?
  6. print(5 + 3)
  7. #ans: 8

Exercises - Part 3

  1. # what does this print?
  2. print("Result:", 10, 20, sep=", ")
  3. #ans: Result:, 10, 20
  4. # what about this?
  5. print()
  6. #ans: (empty line)

Exercises - Part 4

  1. # print with expression?
  2. x = 5
  3. print("x =", x)
  4. #ans: x = 5
  5. # what is the output?
  6. print("Line1\nLine2")
  7. #ans: Line1
  8. #ans: Line2 (on separate lines)

Exercises - Part 5

  1. # tab character?
  2. print("Name:\tAlice")
  3. #ans: Name: Alice (with tab)
  4. # escape quotes?
  5. print("He said \"Hi\"")
  6. #ans: He said "Hi"

Exercises - Part 6

  1. # raw string?
  2. print(r"C:\new\path")
  3. #ans: C:\new\path (backslashes not escaped)
  4. # print return value?
  5. x = print("Hello")
  6. #ans: Hello (printed)
  7. #ans: x is None

Exercises - Part 7

  1. # empty print?
  2. print("")
  3. #ans: (empty line)
  4. # multiple newlines?
  5. print("A\n\nB")
  6. #ans: A
  7. #ans: (blank line)
  8. #ans: B

Google tag (gtag.js)