Python printing method and indentation – with example

  • Reading time:18 mins read

The Python printing method and how to take in user input

In this article, we will explore how to print objects to the terminal and how to take in user input. We will also talk about indentation and whitespaces, which are fundamental in Python.

 

 

The print() function

The print function is the most basic built-in function in Python. It is perhaps the first concept we learn in any programming language. 

The print function allows you to print objects to the terminal or any standard output device. The print() function has multiple optional parameters, and its syntax is as follows.

 

print(object, separator= ’’, end=’’, flush=flush)

 

Where object(s) are the entities that you want to print, these objects are converted to strings before being printed. The separator specifies how the objects are separated, while the end parameter specifies what is to be printed at the end. Finally, the flush parameter has a boolean value indicating if the output is buffered or flushed.

 

python printing

 

We can print different objects to the terminal using the print function. These include strings, integers, floating-point, tuples, dictionaries, etc. In the subsequent examples, we will explore how we can print different types of objects, starting with strings.

Python printing – print a basic string

In Python, a string is a sequence of characters enclosed within a single or double quote mark. To print a string to the terminal, we need to type the word print followed by a pair of parentheses and the string we wish to print between them.

 

 

Both double and single quote marks can be used and will result in the same output.

Python printing – Print out a variable

Alternatively, we can also store a string object in a variable and print it out. This is a better approach since we can print the variable multiple times in your program conveniently.

 

 

Python printing – Print out Integers and floating-point numbers

As mentioned earlier, the print() function accepts different objects regardless of their type.

Printing out integers and floating-point numbers works the same way; the only difference is that numeric types are not wrapped around quote marks.

 

 

Python printing – Printing dictionaries, lists, and tuples

We can also print dictionaries, tuples, and other iterables in Python. For example, we can create a variable storing a tuple and print the tuple using the variable’s name, as shown below.

 

 

This will also work in the same way with lists and dictionaries.

 

python printing

 

Python printing – Printing out user Input

We can also solicit input from a user and print it out using the input() and print() function, respectively. Both of these functions are provided by the Python standard library.

We will create a variable to store the user input and then call the input function with a message to the user, letting them know what to enter as input. Without this message, the terminal will be empty, and the user may not precisely know what to enter as input.

 

 

Indentation and whitespaces in Python

In Python, indentation marks the beginning and end of a code block. Indentation refers to the whitespace at the beginning of a line of code.

Unlike other programming languages such as Java, where indentation is only used for code readability, in Python, it is used for both readability and marking the beginning of a block of code. Improper use of indentation or whitespaces in Python often results in an IndentationError.

In the example below, we will use an if statement to illustrate the importance of indentation. The if statement will check if the user entered the correct input and execute the print function in its body.

 

 

In this case, the indentation marks the beginning of the body of the if statement. If we fail to have an indentation at the start line after the if statement, this example will not work, and we will get an IndentationError instead.

 

 

In Python, four whitespaces are normally used for indentation; however, most IDEs do this automatically. This is the same case with functions where indentation is used to mark the beginning of the function’s body. 

 

python printing

 

Under the indented block, we can also have another loop executed whenever the conditional statement is met. This means that we can nest multiple code blocks as long as they are properly indented. For example, in the script above, we can have a for loop that prints each of the letters of the input entered by the user.

 

 

The for loop indented under the if statement is only executed if the name ‘john’ is entered as the input. Otherwise, the rest of the code is not executed if a different name, say “jack” is entered as the input.

 

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.