print()You already know that print() displays text on the screen. Here are some useful techniques:
You can print several values separated by commas. Python automatically adds a space between them:
name = "Alice"
age = 14
print("My name is", name, "and I am", age, "years old")
Output: My name is Alice and I am 14 years old
+When you use +, you must make sure all values are strings. No spaces are added automatically:
name = "Alice"
print("Hello, " + name + "!")
Output: Hello, Alice!
You can print across multiple lines using triple quotes:
print("""Line one
Line two
Line three""")
input() FunctionThe input() function prompts the user to enter data, which is then stored in a variable:
name = input("What is your name? ")
print("Hello,", name)
When this runs, the program pauses and waits for the user to type something and press Enter.
input() always returns a stringNo matter what the user types, the result is always a string (str). Even if they type a number, it comes back as text:
age = input("How old are you? ")
# age is a string, e.g. "14", not the integer 14
If you need to do arithmetic with the input, you must convert it using int() or float() — we will cover this shortly.
Here is a complete example that asks for the user’s name and favourite food:
name = input("What is your name? ")
print("Nice to meet you,", name)
food = input("What is your favourite food? ")
print("Ah, your favourite food is", food)