LEARN COMPLETE PYTHON IN 24 HOURS
4. Taking Input & Showing Output
In Python, showing output to the user and taking input from them are the most common ways programs interact with people. The two main functions are print() and input().
4.1 print() – All Important Ways to Use It
print() displays text, numbers, variables, or anything on the screen (console/terminal).
Basic Syntax:
Python
print(value1, value2, ..., sep=' ', end='\n')
Key Parameters:
sep → separator between values (default: space ' ')
end → what to print at the end (default: new line \n)
Examples – From Basic to Advanced:
Simple print
Python
print("Hello, World!") # Hello, World! print(100) # 100 print(True) # True
Print multiple values
Python
name = "Anshuman" age = 25 print("Name:", name, "Age:", age) # Output: Name: Anshuman Age: 25
Change separator (sep)
Python
print("Python", "is", "fun", sep="---") # Output: Python---is---fun
Change end (no new line)
Python
print("Hello", end=" ") # No new line print("World!") # Continues on same line # Output: Hello World!
Print special characters
Python
print("Line 1\nLine 2") # New line print("Tab\tHere") # Tab space print("Escape \"quotes\"") # Print quotes
Print variables with commas (old style)
Python
marks = 98 print("You scored", marks, "out of 100") # You scored 98 out of 100
Tip: print() automatically adds a space between items and a new line at the end — very convenient!
4.2 input() – Getting Data from User
input() pauses the program and waits for the user to type something and press Enter.
Syntax:
Python
variable = input("Prompt message: ")
Important: input() always returns a string (even if user types a number).
Examples:
Basic input
Python
name = input("Enter your name: ") print("Hello,", name, "!")
Sample run:
text
Enter your name: Anshuman Hello, Anshuman !
Taking number (but remember — it's string)
Python
age = input("Enter your age: ") print("You are", age, "years old") # If user types 25 → You are 25 years old (but age is string!)
Convert input to number (very common)
Python
age = int(input("Enter your age: ")) # Convert to integer height = float(input("Enter your height in cm: ")) # Convert to float print("Next year you will be", age + 1, "years old") print("Your height in meters:", height / 100)
Sample run:
text
Enter your age: 25 Enter your height in cm: 170.5 Next year you will be 26 years old Your height in meters: 1.705
Multiple inputs in one line
Python
# User types: 10 20 30 numbers = input("Enter 3 numbers separated by space: ").split() print("You entered:", numbers) # ['10', '20', '30']
Warning: If user enters wrong type → error on conversion! Example: int("abc") → ValueError Solution: Use try-except later (advanced topic).
Tip for beginners: Always convert input() when you expect numbers:
Python
marks = int(input("Enter marks: "))
4.3 Formatting Output (f-strings, .format(), % operator)
There are 3 main ways to format (make beautiful) output in Python.
1. f-strings (Recommended – Python 3.6+) – Easiest & Fastest
Python
name = "Anshuman" age = 25 city = "Muzaffarpur" print(f"My name is {name}, I am {age} years old, from {city}.") # My name is Anshuman, I am 25 years old, from Muzaffarpur.
Advanced f-string tricks:
Python
price = 999.99 print(f"Price: ₹{price:.2f}") # Price: ₹999.99 (2 decimal places) percentage = 78.456 print(f"Score: {percentage:.1f}%") # Score: 78.5% today = "2026" print(f"Welcome to Python in {today:^20}!") # Center align # Welcome to Python in 2026 !
2. .format() method – Still used a lot
Python
name = "Anshuman" age = 25 print("My name is {}, I am {} years old.".format(name, age)) # My name is Anshuman, I am 25 years old. print("Hello {0}, welcome back {0}! You are {1} now.".format(name, age)) # Hello Anshuman, welcome back Anshuman! You are 25 now.
3. % operator (Old style – Avoid in new code)
Python
name = "Anshuman" marks = 98.5 print("Name: %s, Marks: %.1f" % (name, marks)) # Name: Anshuman, Marks: 98.5
Comparison Table (Quick Guide):
MethodSyntax ExampleBest ForModern?f-stringsf"Hello {name}"Everyday use, clean & fastYes (2016+).format()"Hello {}".format(name)Older code, dynamic stringsYes% operator"Hello %s" % nameVery old codeNo
Recommendation for 2026: Always use f-strings — they are the most readable and fastest.
Mini Project Example – Combining print + input + formatting
Python
name = input("What is your name? ") age = int(input("How old are you? ")) print("-" 40) print(f"Welcome, {name.upper()}!") print(f"You are {age} years old → Next birthday: {age + 1}") print(f"Enjoy learning Python in {2026} 🎉") print("-" 40)
Sample Output:
text
What is your name? Anshuman How old are you? 25 ---------------------------------------- Welcome, ANSHUMAN! You are 25 years old → Next birthday: 26 Enjoy learning Python in 2026 🎉 ----------------------------------------
This completes the full Taking Input & Showing Output section.
Email-ibm.anshuman@gmail.com
© 2026 CodeForge AI | Privacy Policy |Terms of Service | Contact | Disclaimer | 1000 university college list|book library australia 2026
All my books are exclusively available on Amazon. The free notes/materials on globalcodemaster.com do NOT match even 1% with any of my PUBLISHED BOoks. Similar topics ≠ same content. Books have full details, exercises, chapters & structure — website notes do not.No book content is shared here. We fully comply with Amazon policies.