βΊοΈ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:
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:
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).
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:
Output:
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:
This code will output Hello, world!
to the console. You can also pass in multiple values to print()
by separating them with commas:
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:
This code will output:
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:
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 outputsys.stdout
.flush
: a boolean value indicating whether to flush the stream. It is optional, and the default value isFalse
.
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:
Output:
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:
Output:
And here's an example of using f-strings:
Output:
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:
Output:
Output formatting can make your program's output more readable and easier to understand, especially when dealing with large amounts of data.
Last updated