Python Input and Output

Video Tutorial
FREE
Input Function in Python thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

In Python, program output is displayed using the print() function, while user input is obtained with the input() function. Python treats all input as strings by default, requiring explicit conversion for other data types.

Taking Input from User in Python

The programs were not too interactive as we had hard-coded values of the variables. Sometimes, users or developers want to run the programs with their own data in the variables for their own ease.

To execute this, we will learn to take input from the users, in which the users themselves will define the values of the variables.

The input() statement allows us to do such things in Python.

The syntax for this function is as follows:

Here, prompt is the string we want to display while taking input from the user. This is optional.

Examples

1. user input with a message

You can provide a prompt message as an argument to the input() function. This message is displayed to the user before waiting for the input.

Output:

2. Integer input in Python

Output:

Note: By default, the python input() function takes the user’s input as a string. In example 2, we had to convert the age (from string to integer), which was inputted by the user, using the int() along with the input function.

We again need to convert the integer (‘new’ variable) into a string for concatenation during printing the output.

Note: Instead of int(), we can also use the float() function, allowing users to enter decimal numbers instead of integers.

Taking Multiple Inputs in Python

Taking multiple inputs from a user in a single line in Python can be efficiently handled using the input() function combined with string methods like split(). This is particularly useful when you need to process several pieces of data entered at once. Here is an example to take multiple inputs:

Taking inputs for the Sequence Data Types like List, Set, Tuple

Taking inputs for sequence data types like lists, sets, and tuples in Python involves receiving input from the user and then converting it into the desired data type. Let's see some examples to understand this:

List

Set

Tuple

Displaying Output in Python

We use the widely used print() statement to display some data on the screen.

We can output the particular data on some device(screen) or even in some files.

While writing real-world programs, we have to write statements that explicitly output numbers and strings.

Let’s take an example to display a simple text.

Output:

Don’t worry much about double or single quotes. Either of them will work fine in Python.

Before learning more about output in Python, let’s understand the actual syntax of the print() function. This function takes some additional arguments other than objects/values that offer more control over the output format.

For your better understanding of the syntax here, we have defined a few keywords of the above statement:

  • object(s) are the values to be printed on the screen. They are converted to strings before getting printed.
  • sep keyword is used to specify how to separate the objects inside the same print statement. By default, we have it as sep=' ', a space between two objects.
  • end is used to print a particular thing after all the values are printed. By default, we have end as \n, which provides a new line after each print() statement.
  • file is used to specify where to display the output. By default, it is sys.stdout (which is the screen).
  • flush specifies the boolean expression if the output is False or True. By default, it is False. In Python, the output from the print() goes into a buffer. Using the flush= True parameter helps in flushing the buffer as soon as we use the print() statement.

Let’s take some examples to understand using the print() function’s syntax.

Python Print Output

Output:

Note: All the values are followed by a comma( , ), distinguishing between themselves as separate values/objects. By default, this adds a space between the values printed as output.

Python print() with end Parameter

The end parameter in the print() function specifies what to print at the end of the output. By default, it is a newline character (\n), but you can set it to a different string.

Output:

In this example, the end=" " parameter replaces the newline with a space, so the two print statements output on the same line.

Python print() with sep parameter

The sep parameter defines the separator between multiple items passed to the print() function. By default, it is a space.

Output:

In this example, the sep=", " parameter specifies that a comma followed by a space should be used as the separator between items.

Formatting Output

Sometimes, a user might want to make the output of a code prettier and more attractive to the audience. For this, we have some other inbuilt functions.

1. Using formatted string literals

Formatted string literals, commonly known as f-strings, were introduced in Python 3.6. They allow you to embed expressions inside string literals using curly braces {} and a prefix f. Let's take an example:

Output:

2. The String format() Method

We can use the str.format() method for string formatting in Python. This format() function will make the output more presentable.

The following curly braces { } in the code acts as placeholders. We can also specify the order of these variables by putting numbers in the placeholders.

Output:

3. Using % Operator

We can also use the old % method, which we used similarly in C language.

Output:

Note: Though other methods are available for formatting, the string modulo operator is still widely used by users.

4. Manual String Formatting

Manual string formatting involves using string operations like concatenation and methods like str.ljust(), str.rjust(), and str.center() to format strings.

Output:

Conclusion

  • Explored a variety of techniques for data input and output in Python, providing a comprehensive understanding of these essential processes.
  • Each method is explained in a detailed, step-by-step manner, accompanied by straightforward and easy-to-understand examples.
  • The use of code snippets in the article aids in clearly demonstrating how to handle input and output in Python.
  • Highlighted the concise and efficient nature of Python syntax, especially in comparison to other programming languages like C++ and Java, which require additional elements like header files.
  • Emphasized the importance of practising different methods to enhance coding skills in Python, suggesting that engaging with various problems can deepen understanding of programming logic.
  • Aimed to clear up common doubts and provide informative and educational content, making the article a valuable resource for learners at various levels.

Read More:

  1. Python Modules
  2. Format in Python
  3. String Formatting in Python
  4. int() Function in Python