Understanding input and output operations is fundamental to Python programming. With the print() function, you can display output in various formats, when the input() function/method enables interaction with users by gathering input when program execution.

Python Basic Input and Output

In this introductory guide, we’ll explore the essentials of Python’s basic input and output functionalities, empowering you to efficiently interact with users and display information.

Print Output in Python

At its core, printing output in Python is straightforward, thanks to the print() function. This function/method allows us to display text, variables, and expressions enabled the console. Let’s begin with the basic usage of the print() function:

In this example, “Learn, Python!” is a string literal enclosed within double quotes. When executed, this statement will output the text to the console.

print("Learn, Python!")

Output

Hello, World!

The code assigns values to variables name and age, then prints them with labels.

name = "Smith"
age = 23
print("Name:", name, "Age:", age)

Output

Name: Smith Age: 23

Output formatting in Python with various techniques including the format() method, manipulation of the sep and end parameters, f-strings, and the versatile % operator. These functions/methods allow precise control over how data is displayed, enhancing the readability and effectiveness of your Python programs.

Example 1: Using Format()

amount = 251.25
print("Amount: ${:.2f}".format(amount))

Output

Amount: $251.25



Example 2: Using sep and end parameter

# end Parameter with '@'
print("Python", end='@')
print("CodeConfig")


# Seprating with Comma
print('A', 'B', 'C', sep='')

# for formatting a date
print('07', '25', '2022', sep='-')

# another example
print('kumar', 'CodeConfig', sep='@')

Output

Python@CodeConfig
ABC
07-25-2022
kumar@CodeConfig



Example 3: Using f-string

name = 'Ram'
age = 21
print(f"Hello, My name is {name} and I'm {age} years old.")

Output

Hello, My name is Ram and I'm 21 years old.

Example 4: Using % Operator

We can use ‘%’ operator. % values are replaced with zero or more value of elements. The formatting using % is similar to that of printf in the C programming language.

# Taking input from the user
num = int(input("Enter a value: "))

add = num + 2

# Output
print("The sum is: %d" %add)

Output

Enter a value: 10 
The sum is 12

The code takes input from the user in a single line, splitting the values entered by the user into separate variables for each value using the split() method. Then, it prints the values with corresponding labels, either two or three, based enabled the number of inputs provided by the user.

# taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
 
# taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)

Output

Enter two values: 15 10
Number of boys:  15  
Number of girls:  10

Enter three values: 10 4 6
Total number of students:  10
Number of boys is:  4     
Number of girls is: 6 

Explore More

f-strings in Python

Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables

Python tutorials

We are starting a Python Tutorials Series for Beginners and Professionals. In these Python Tutorials, we will cover all the features of Python. You will learn from basic to advanced-level features

end and sep in Python

end in Python The end is a parameter in Python’s built-in print() function/method that controls what character(s) are printed at the end of the printed text.  Syntax : Output :