Python provides built-in functions for taking user input and displaying output.
input()
: Used for taking user input.print()
: Used for displaying output.
1. Taking Input Using input()
The input()
function allows the user to enter data. By default, it treats input as a string.
Syntax:
variable = input("Message to user: ")
Example 1: Taking User Input
name = input("Enter your name: ")
print("Hello,", name)
Output:
Enter your name: Alice
Hello, Alice
2. Converting Input Data Type
Since input()
always returns a string, you must convert it for numeric calculations.
Example 2: Taking Integer Input
age = int(input("Enter your age: "))
print("Next year, you will be", age + 1)
Output:
Enter your age: 25
Next year, you will be 26
int(input())
converts the input to an integer.
Example 3: Taking Float Input
price = float(input("Enter the price: "))
print("Total price with tax:", price * 1.1)
Output:
Enter the price: 100.5
Total price with tax: 110.55
float(input())
converts the input to a floating-point number.
3. Displaying Output Using print()
The print()
function outputs data to the console.
Syntax:
print(value1, value2, ..., sep=" ", end="\n")
sep
: Specifies the separator between values (default: space).end
: Defines what to print at the end (default: newline).
Example 4: Basic Output
print("Hello, World!")
Output:
Hello, World!
Example 5: Printing Multiple Values
name = "Alice"
age = 25
print("Name:", name, "Age:", age)
Output:
Name: Alice Age: 25
4. Formatting Output
Python offers several ways to format output.
(a) Using f-strings (Python 3.6+)
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Alice and I am 25 years old.
(b) Using format()
Method
name = "Alice"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
Output:
My name is Alice and I am 25 years old.
(c) Using %
Formatting (Old Method)
name = "Alice"
age = 25
print("My name is %s and I am %d years old." % (name, age))
Output:
My name is Alice and I am 25 years old.
5. Customizing print()
Output
(a) Changing Separator (sep
)
print("Apple", "Banana", "Cherry", sep=", ")
Output:
Apple, Banana, Cherry
(b) Changing End Character (end
)
print("Hello", end=" ")
print("World!")
Output:
Hello World!
- The second
print()
appears on the same line becauseend=" "
prevents a new line.
6. Printing Special Characters
Python uses escape sequences for special characters.
Example 6: Using Escape Sequences
print("Hello\nWorld!") # Newline
print("Tab\tSpace") # Tab space
print("This is a backslash: \\") # Backslash
print("She said, \"Hello!\"") # Double quotes inside string
Output:
Hello
World!
Tab Space
This is a backslash: \
She said, "Hello!"
7. Printing to a File
You can redirect output to a file using the file
parameter.
Example 7: Writing Output to a File
with open("output.txt", "w") as file:
print("Hello, File!", file=file)
- This creates a file output.txt and writes
"Hello, File!"
to it.
Conclusion
input()
takes user input as a string.- Convert input using
int()
orfloat()
if needed. print()
displays output with options likesep
andend
.- Use f-strings or
format()
for formatted output. - Escape sequences handle special characters like
\n
,\t
, and\"
. - Output can be redirected to a file.