Python Tutorial for Beginners #5 - User Input & Output (input(), print(), Mini Project)
Video: Python Tutorial for Beginners #5 - User Input & Output (input(), print(), Mini Project) by Taught by Celeste AI - AI Coding Coach
Watch full page →Python User Input and Output: input(), print(), and a Temperature Converter Project
This tutorial covers how to interact with users in Python using the input() function to gather input and the print() function to display output. You will also learn how to format printed text with parameters like sep and end, use f-strings for alignment and formatting, and apply these concepts in a mini project that converts temperatures between Celsius and Fahrenheit.
Code
# Get user input as a string
name = input("Enter your name: ")
# Convert input to integer
age = int(input("Enter your age: "))
# Convert input to float
height = float(input("Enter your height in meters: "))
# Print with default separator (space) and end (newline)
print("Name:", name, "Age:", age, "Height:", height)
# Print with custom separator and end
print("Python", "is", "fun", sep=" - ", end="!\n")
# Using f-strings for formatted output
score = 92.4567
print(f"Score: {score:.2f}") # 2 decimal places
# Align text in f-strings
print(f"|{'Name':<10}|{'Age':^5}|{'Height':>7}|")
print(f"|{name:<10}|{age:^5}|{height:>7.2f}|")
# Mini Project: Temperature Converter
# Convert Celsius to Fahrenheit and vice versa
temp_input = input("Enter temperature (e.g., 36.6C or 98F): ").strip()
if temp_input[-1].upper() == "C":
celsius = float(temp_input[:-1])
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius:.1f}°C is {fahrenheit:.1f}°F")
elif temp_input[-1].upper() == "F":
fahrenheit = float(temp_input[:-1])
celsius = (fahrenheit - 32) * 5 / 9
print(f"{fahrenheit:.1f}°F is {celsius:.1f}°C")
else:
print("Invalid input. Please end with 'C' or 'F'.")
Key Points
- The
input()function reads user input as a string, which can be converted to other types like int or float. - The
print()function’ssepparameter changes the separator between items, andendchanges what prints at the end. - F-strings allow formatting numbers (e.g., decimal places) and aligning text with
<,^, and>inside the braces. - Simple parsing of user input can be used to build interactive mini projects, such as a temperature converter that handles Celsius and Fahrenheit.
- Always validate and clean user input to handle unexpected formats gracefully.