The two most essential functions for beginner Python developers! print() displays information on screen, and input() reads data from the user. Together they make programs interactive!
You can run code in a local terminal, an IDE, or a browser sandbox. The output area name and location depend on the tool: look for Console, Terminal, or Output. Use its label rather than relying on panel color or position.
๐จ๏ธ The print() function โ display text
Basic usage
print('Hello, world!')
print("Python is easy!")
print('You can use "double" quotes inside single ones')
Multiline text and triple quotes
Text spanning several lines can be written between triple single ''' or triple double """ quotes:
message = """First line
Second line
Third line"""
print(message)
The opening and closing quote styles must match. Newlines and spaces inside the string are preserved.
For multiline text containing backslashes, a raw string is convenient. Add r before the opening quotes:
folders = r"""project\code
project\data
project\tests
"""
print(folders)
A raw string does not turn backslashes into escape sequences. It still cannot end with an odd number of backslashes immediately before the closing quotes.
When to use an r-string
The r prefix is useful when text contains many backslashes:
# Regular string: every backslash must be escaped
windows_path = 'C:\\Users\\student\\project'
# Raw string: backslashes are preserved
windows_path = r'C:\Users\student\project'
print(windows_path)
Raw strings are also convenient for ASCII art and regular-expression patterns. r changes backslash processing, but it does not remove quote rules. If the text contains double quotes, it is convenient to delimit the string with single quotes:
quote = r'They said: "Hello"'
Do not add r to every string automatically. Use a regular string for ordinary text, an f-string for value substitution, and a raw string when backslashes must be preserved.
Printing multiple values
name = 'Alex'
age = 16
# Comma-separated โ spaces are added automatically
print('My name is', name, 'and I am', age, 'years old')
# Output: My name is Alex and I am 16 years old
Printing variables of different types
name = 'Maria'
age = 20
height = 1.65
is_student = True
print(name) # Maria
print(age) # 20
print(height) # 1.65
print(is_student) # True
f-strings โ the modern way
name = 'Maxim'
age = 18
# f-string โ cleaner and more convenient!
print(f'Hi, {name}! You are {age} years old.')
print(f'In a year you will be {age + 1}')
print(f'One minute = {60} seconds, one hour = {60 * 60} seconds')
Controlling output
# By default print() adds a newline
print('First line')
print('Second line')
# No newline at the end
print('Hello', end=' ')
print('world!') # Output: Hello world!
# Blank line
print() # Just a newline
# Custom separator
print('apple', 'banana', 'orange', sep=', ')
# Output: apple, banana, orange
๐ฅ The input() function โ reading user input
Basic usage
# The program pauses and waits for input
name = input('What is your name? ')
print(f'Hi, {name}!')
Important! input() ALWAYS returns a string (str)!
Type conversion
# โ Wrong โ we get a string
age = input('How old are you? ')
print(age + 5) # Error! Can't add a string and a number
# โ
Correct โ convert to a number
age = int(input('How old are you? '))
print(f'In 5 years you will be {age + 5}')
# For decimal numbers
height = float(input('Your height in meters: '))
print(f'Your height: {height} m')
An interactive program
print('=== BMI Calculator ===')
# Collect data
weight = float(input('Enter your weight (kg): '))
height = float(input('Enter your height (m): '))
# Calculate
bmi = weight / (height ** 2)
# Display result
print(f'\nYour BMI: {bmi:.1f}')
if bmi < 18.5:
print('Underweight')
elif bmi < 25:
print('Normal weight')
else:
print('Overweight')
๐ฏ Common errors and fixes
Error 1: SyntaxError โ an unclosed string or parenthesis
# โ Missing closing parenthesis
print('Hello' # Error!
# โ
Close the parenthesis
print('Hello')
Error 2: NameError โ Python treats text as a name
# โ No quotes: Python looks for a variable named Hello
print(Hello)
# โ
Text is wrapped in quotes
print('Hello')
# โ Incorrect function-name capitalization
Print('Hi')
# โ
Names are case-sensitive
print('Hi')
Error 3: TypeError when adding
# โ Adding a string and a number
age = input('Age: ') # This is a string!
print(age + 5) # TypeError!
# โ
Convert to int first
age = int(input('Age: '))
print(age + 5) # Works!
Error 4: ValueError
# If the user types text instead of a number
age = int(input('Age: '))
# Entered "abc" โ ValueError!
# For now, just warn the user
print('Please enter a NUMBER!')
๐ก Useful examples
Greeting
print('๐ Welcome!')
name = input('What is your name? ')
hobby = input('What are your hobbies? ')
print(f'\nNice to meet you, {name}!')
print(f'{hobby} โ that is awesome! ๐')
Simple quiz
print('=== Python Quiz ===\n')
score = 0
answer = input('What does print(2 + 2) output? ')
if answer == '4':
print('โ
Correct!\n')
score += 1
else:
print('โ Wrong, the answer is: 4\n')
answer = input('True or False โ Python is case-sensitive? ')
if answer.lower() == 'true':
print('โ
Correct!\n')
score += 1
else:
print('โ Wrong, the answer is: True\n')
print(f'Your score: {score}/2')
Currency converter
print('๐ฐ Currency Converter (USD โ EUR)\n')
rate = 0.92 # USD to EUR exchange rate
dollars = float(input('Enter the amount in dollars: '))
euros = dollars * rate
print(f'\n${dollars} = โฌ{euros:.2f}')
๐ Cheat sheet
# Output
print('text') # Simple output
print(variable) # Print a variable
print('a', 'b', 'c') # Multiple values
print(f'x = {x}') # f-string
print('text', end='') # No newline
print() # Blank line
# Input
text = input('Question: ') # Read a string
number = int(input('Number: ')) # Read an integer
decimal = float(input('Decimal: ')) # Read a float
# Type conversions
str(123) # '123' (number โ string)
int('456') # 456 (string โ integer)
float('1.5') # 1.5 (string โ float)
๐ Practice
Try building:
1. A survey โ collect name, age, city, hobbies
2. A calculator โ two numbers and an operation
3. A story generator โ ask for words and build a narrative
4. A converter โ Celsius to Fahrenheit
๐ Summary
- print() โ displays information on screen
- input() โ reads data from the user (always returns a string!)
- f-strings โ a convenient way to embed variables in text
- int() / float() โ convert strings to numbers
With these two functions you can build interactive programs! ๐ช
๐ฌ Comments (0)
No comments yet
Be the first to share your opinion about this article!