DevHireLab
Tutorials
BootcampProblemsCode SimulatorAI InterviewSoonContact
DevHireLab
Tutorials
BootcampProblemsCode SimulatorAI InterviewSoonContact
Getting Started
01
What is Python and Why Learn It in 2026
02
Installing Python on Windows, Mac, and Linux
03
Setting up VS Code / PyCharm for Python
04
Understanding the Python Interpreter and REPL
05
Writing and Running Your First Python Program
06
Python Syntax and Indentation Rules
Variables and Data Types
01
Variables and Naming Conventions in Python
02
Numbers in Python (int, float, complex)
03
Strings and String Methods in Python
04
Booleans in Python
05
Type Conversion / Casting in Python
06
Understanding the None Type in Python
Operators
01
Arithmetic Operators in Python
02
Comparison Operators in Python
03
Logical Operators in Python
04
Assignment Operators in Python
05
Bitwise Operators in Python
06
Identity (is) and Membership (in) Operators
07
Walrus Operator (:=) in Python
Input, Output & Strings
01
input() and print() Functions in Python
02
f-strings and String Formatting in Python
03
String Slicing and Indexing in Python
04
Common String Methods (split, join, strip, replace)
05
Escape Characters in Python Strings
Control Flow
01
if, elif, else Statements in Python
02
Nested Conditionals in Python
03
for Loops in Python
04
while Loops in Python
05
break, continue, and pass Statements
06
Loop with else Clause in Python
07
match-case (Structural Pattern Matching)
Data Structures
01
Python Lists: Creation, Indexing, Slicing, Methods
02
Python Tuples and Immutability
03
Python Sets and Set Operations
04
Python Dictionaries: Keys, Values, and Methods
05
List Comprehensions in Python
06
Dictionary & Set Comprehensions in Python
07
Working with Nested Lists and Dictionaries
Functions
01
Defining and Calling Functions in Python
02
Function Arguments: Positional, Keyword, Default
03
*args and **kwargs in Python
04
Return Statements in Python Functions
05
Lambda Functions in Python
06
Recursion in Python
07
Variable Scope: Local, Global, Nonlocal
08
Docstrings and Function Annotations
Functional Programming Basics
01
map(), filter(), and reduce() in Python
02
Closures in Python Explained
03
Python Decorators: Basic to Advanced
04
Generators and yield in Python
Object-Oriented Programming (OOP)
01
Classes and Objects in Python
02
The init Constructor in Python
03
Instance vs Class Variables in Python
04
Inheritance in Python (Single, Multiple, Multilevel)
05
Polymorphism in Python
06
Encapsulation in Python
07
Abstraction in Python with the abc Module
08
Magic/Dunder Methods in Python
09
Static and Class Methods in Python
10
The @property Decorator in Python
11
Python Dataclasses Explained
Error Handling
01
try, except, finally in Python
02
Handling Multiple Exceptions in Python
03
Raising Custom Exceptions in Python
04
Understanding Python's Exception Hierarchy
05
Assertions in Python
File Handling
01
Reading and Writing Text Files in Python
02
Working with the with Statement in Python
03
Working with CSV Files in Python
04
Working with JSON Files in Python
05
Working with Directories (os, pathlib) in Python
Modules and Packages
01
Importing Built-in Modules in Python
02
Creating Your Own Modules in Python
03
Understanding Packages and init.py
04
Python Standard Library Overview
05
Installing Packages with pip
Advanced Core Concepts
01
Iterators and Iterables in Python
02
Context Managers in Python (with, contextlib)
03
Multithreading Basics in Python
04
Multiprocessing Basics in Python
05
Asyncio Basics: async/await in Python
06
Memory Management and Garbage Collection in Python
07
Type Hints in Python (typing module)

input() and print() Functions in Python

Every interactive Python program comes down to two functions working together: the python print function sends output to the console, and the python input function captures whatever the user types back. You've used both already in earlier articles in this series — this one goes deeper into the parameters and patterns that make them genuinely useful beyond the basics.

Introduction: How Python Talks to the User

print() and input() are, in a sense, opposite ends of the same conversation. print() sends information out to the console; input() waits and listens for something to come back in. Together, they're the foundation of any program that actually interacts with the person running it, rather than just processing fixed data silently.

name = input("What's your name? ")
print(f"Hello, {name}!")

That's the whole loop — ask, wait, respond — and it's worth understanding each half in more detail.

The print() Function

Basic usage

print() can display strings, numbers, variables, or several items at once, separated by commas:

print("Hello, World!")
print(42)

name = "Alex"
age = 30
print(name, age)   # Alex 30

When you pass multiple comma-separated arguments, print() automatically inserts a space between them and converts non-string values to strings for you.

The sep parameter

By default, print() separates multiple arguments with a single space — but you can change that with the sep parameter:

print("2026", "07", "09", sep="-")
# 2026-07-09

print("apple", "banana", "cherry", sep=", ")
# apple, banana, cherry

This is genuinely useful for building formatted output — dates, CSV-style rows, anything with a consistent delimiter — without manually concatenating strings.

The end parameter

By default, print() ends every call with a newline character, which is why each call produces its own line. The end parameter lets you override that:

print("Loading", end="")
print(".", end="")
print(".", end="")
print(".")
# Loading...

All four calls print onto the same line, because each one overrides the default newline with either an empty string or, in the last call, the default newline to finally move to the next line. This is the standard way to achieve print without newline python behavior — useful for progress indicators, building a line incrementally, or printing values with custom spacing.

file and flush, briefly

Two more parameters exist worth knowing about, even if you won't reach for them often as a beginner:

  • file — redirects output somewhere other than the console, such as a file object opened for writing.

  • flush — forces the output to be written immediately rather than potentially being buffered, which occasionally matters for real-time logging or progress output.

print("Processing...", flush=True)

Most everyday scripts never need either of these, but they're there when you need finer control over where and when output actually appears.

The input() Function

Basic usage

input() displays an optional prompt, then pauses your program until the user types something and presses Enter:

name = input("Enter your name: ")
print(f"Hi, {name}")

Whatever the user typed is stored in the variable — in this case, name.

The critical concept: input() always returns a string

This is the single most important thing to understand about input(), and it's a mistake almost every beginner makes at least once: no matter what the user types — even if it's clearly a number — input() always returns it as a string.

age = input("Enter your age: ")
print(type(age))   # <class 'str'> — even if the user typed "25"

If you try to do arithmetic on that value directly, you'll run into an error:

age = input("Enter your age: ")
next_year = age + 1
# TypeError: can only concatenate str (not "int") to str
Converting input for calculations

The fix is explicit type conversion, using int() or float() as appropriate — a concept covered in more depth in the earlier article on type conversion:

age = int(input("Enter your age: "))
next_year = age + 1
print(f"Next year you'll be {next_year}")

Wrapping the input() call directly in int() like this is an extremely common pattern — it converts the value the moment it's captured, so you don't have to remember to do it separately later.

Combining input() and print() in Practice

A simple interactive program

Here's a small program that ties both functions together, asking for two pieces of information and responding based on them:

name = input("What's your name? ")
age = int(input("How old are you? "))

print(f"Nice to meet you, {name}!")
print(f"In 10 years, you'll be {age + 10} years old.")
Using f-strings for clean output

As covered in the earlier strings article, f-strings are the cleanest way to combine captured input with surrounding text — no manual string concatenation or type conversion needed just for display purposes:

city = input("Where do you live? ")
print(f"{city} sounds like a great place to be!")
Handling multiple inputs on one line

Sometimes you want to collect more than one value from a single line of input, rather than prompting separately for each. Combining input() with .split() handles this cleanly:

name, age = input("Enter your name and age, separated by a space: ").split()
age = int(age)

print(f"{name} is {age} years old")

Here, .split() breaks the single line of input into a list of substrings (splitting on whitespace by default), and Python's multiple assignment — covered in the earlier article on assignment operators — unpacks that list directly into name and age in one line. Note that age still comes out as a string from .split(), so it still needs an explicit int() conversion afterward.

Common Mistakes and Best Practices

Forgetting to convert input() before doing math

This is, by a wide margin, the most common mistake beginners make with these two functions together:

number = input("Enter a number: ")
doubled = number * 2
print(doubled)
# If the user enters "5", this prints "55" — string repetition, not multiplication!

Because number is a string, * 2 doesn't multiply it numerically — it repeats the string, which is valid Python but almost certainly not what was intended. The fix, as before, is converting explicitly: number = int(input("Enter a number: ")).

Overusing sep and end

sep and end are genuinely useful, but it's easy to reach for them in places where they just add noise rather than clarity. Use them purposefully — building a table, a progress indicator, or CSV-like output are all good reasons. Sprinkling custom sep/end values into ordinary, one-off print statements usually just makes the code harder to scan for no real benefit.

# A reasonable use — building a simple table row
print("Name", "Score", sep="\t")
print("Alex", 95, sep="\t")
Validating user input

Real programs can't always trust that a user typed exactly what was expected. If you're converting input() directly with int() or float(), wrap it in a try/except block to handle the case where the input isn't actually numeric — a pattern covered in more depth in the earlier article on type conversion:

try:
    age = int(input("Enter your age: "))
    print(f"You are {age} years old")
except ValueError:
    print("That doesn't look like a valid number.")

This small habit is the difference between a program that crashes on unexpected input and one that handles it gracefully — a distinction that matters more and more as your programs grow beyond simple scripts.

PREVIOUSNEXT LESSON