☺️Input and Output

In this tutorial, we will learn simple ways to display output to users and take input from users in Python with the help of examples

In Python, you can take input from the user using the input() function and output data using the print() function. These are the two most commonly used functions for basic input and output operations in Python.

Python input()

input() function is a built-in function in Python that reads a line from the user and returns it as a string.

Syntax:

input([prompt])

The input() function takes one optional parameter prompt, which is a string that is displayed to the user before they enter the input.

Parameters:

  • prompt (optional): A string that is displayed to the user before they enter the input. If this parameter is not provided, the function waits for the user to input any value.

Return Value:

The input() function returns the user input as a string.

Here is an example of using the input() function:

name = input("What is your name? ")
print("Hello, " + name + "!")

In this example, the input() function prompts the user to enter their name, which is stored in the name variable. The print() function then outputs a personalized greeting to the user.

Note that the value returned by input() is always a string, so if you need to perform numeric operations on the input, you must convert it to the appropriate data type (e.g. using the int() or float() functions).

age = int(input("What is your age? "))
print("You will be " + str(age + 10) + " in 10 years.")

In this example, the input() function prompts the user to enter their age, which is converted to an integer using the int() function. The program then adds 10 to the user's age and prints the result as a string using the str() function.

Example 1: Python Print Statement

Here's an example of the Python print() statement:

# Simple print statement
print("Hello, World!")

# Print with a variable
message = "Welcome to Python Programming!"
print(message)

# Print with multiple values
name = "John"
age = 25
print("My name is", name, "and I am", age, "years old.")

Output:

Hello, World!
Welcome to Python Programming!
My name is John and I am 25 years old.

Python Output

In Python, the print() function is used to output values to the console. The print() function can take one or more values separated by commas as arguments, which it then outputs to the console.

Here's an example:

print("Hello, world!")

This code will output Hello, world! to the console. You can also pass in multiple values to print() by separating them with commas:

name = "Alice"
age = 25
print("My name is", name, "and I am", age, "years old.")

This code will output My name is Alice and I am 25 years old. to the console.

You can also use escape sequences to format the output. For example, to output a newline character, use the escape sequence:

print("First line\nSecond line")

This code will output:

First line
Second line

There are many other escape sequences you can use, such as for a tab character and \\ to output a backslash. You can also use string formatting to format the output in a more structured way.

Syntax of print()

The syntax of the print() function in Python is:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
  • objects: zero or more objects to be printed.

  • sep: separator between the objects. It is optional, and the default value is a single space character ' '.

  • end: end string after the last object is printed. It is also optional, and the default value is a newline character '\n'.

  • file: the object where the printed output will be sent. It is optional, and the default value is the standard output sys.stdout.

  • flush: a boolean value indicating whether to flush the stream. It is optional, and the default value is False.

The print() function can take multiple arguments separated by a comma and print them one after the other. The values can be of any data type, including strings, numbers, and variables.

For example:

print("Hello, World!")
print(42)
print("My name is", "John", "Doe")

Output:

Hello, World!
42
My name is John Doe

Output formatting

Output formatting refers to how the output of a program is presented to the user. In Python, the built-in print() function allows you to format the output in various ways.

One way to format output is by using string formatting. Python provides a number of ways to format strings, such as using the % operator, the format() method, and f-strings.

Another way to format output is by using escape characters. Escape characters are special characters that allow you to include non-printable characters in your output, such as newlines, tabs, and backslashes.

Here's an example of using string formatting with the % operator:

name = "John"
age = 30
print("My name is %s and I am %d years old." % (name, age))

Output:

My name is John and I am 30 years old.

And here's an example of using f-strings:

name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")

Output:

My name is John and I am 30 years old.

You can also use escape characters to format your output. For example, you can use to insert a newline character, to insert a tab character, and \\ to insert a backslash:

print("First line\nSecond line")
print("Tabbed\toutput")
print("Backslash \\")

Output:

First line
Second line
Tabbed    output
Backslash \

Output formatting can make your program's output more readable and easier to understand, especially when dealing with large amounts of data.

Last updated