How do you indent print statements in python?

I think what you're looking for here is the textwrap module:

user = "Username"
prefix = user + ": "
preferredWidth = 70
wrapper = textwrap.TextWrapper(initial_indent=prefix, width=preferredWidth,
                               subsequent_indent=' '*len(prefix))
message = "LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT " * 3
print wrapper.fill(message)

This prints:

Username: LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT
          LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT
          LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT

If you actually want to use tabs in the indent, that's a little trickier, because you have to first tab-expand the initial_indent to figure out the correct subsequent_indent to use. And, because your prefix actually ends with two tabs, it's even more complicated. Here's the simplest I've come up with:

user = "Username"
prefix = user + ":\t\t"
expanded_indent = textwrap.fill(prefix+'$', replace_whitespace=False)[:-1]
subsequent_indent = ' ' * len(expanded_indent)
wrapper = textwrap.TextWrapper(initial_indent=prefix,
                               subsequent_indent=subsequent_indent)
message = "LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT " * 3
print wrapper.fill(message)

If you do this repeatedly, you will probably want to wrap that mess up in a function.

  • The Python printing method and how to take in user input
  • The print() function
  • Python printing – print a basic string
  • Python printing – Print out a variable
  • Python printing – Print out Integers and floating-point numbers
  • Python printing – Printing dictionaries, lists, and tuples
  • Python printing – Printing out user Input
  • Indentation and whitespaces in Python
  • Summary
  • Programming tutorials
  • Learn to code and change your career!

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.

How do you indent print statements in python?

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.

How do you indent print statements in python?

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. 

How do you indent print statements in python?

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

  • How to use the Python for loop
  • How to use Python Sets
  • How to use a Python Dictionary
  • How to use Python Classes
  • How to use Python Range
  • How to use Python if-else statements
  • How to use Python RegEx
  • How to use Python Lists
  • How to use Python Enumerate
  • How to use Python Functions
  • How to use Python Split
  • How to use Python Try-Except
  • How to use Python Tuples
  • How to use Python Arrays
  • How to use Python Sort
  • How to use the Python DateTime
  • How to download Python?
  • How to use the Python FileWrite function
  • How to use Python Lambda
  • How to use Python ListAppend
  • How to use Python ListComprehension
  • How to use Python Map
  • How to use Python Operators
  • How to use Python Pandas
  • How to use Python Requests
  • How to use Python Strings
  • How to use Python Count
  • How to use Python Comments
  • How to use the Python File Reader method
  • How to use the Python IDE-s
  • How to use Python logging
  • How to use Python Print
  • How to use the Python Zip
  • How to use Python Append
  • How to use Python Global Variables
  • How to use the Python join method
  • How to use Python list length
  • How to use Python JSON files
  • How to use Python Modulo
  • How to use Python file opening methods
  • How to use Python round
  • How to use Python sleep
  • How to use Python replace
  • How to use Python strip
  • How to use the Python Time module
  • How to use Python unittests
  • How to save data to a text file using Context Manager?
  • How to use Python external modules
  • How to use Python find
  • How to install the Python pip package manager
  • How to delete files in Python
  • Parsing XML files in Python
  • How to make a GUI in Python
  • How to use Python in Command Prompt
  • How to Run a Python Program in VS Code
  • How to run a program in Python IDLE
  • How to run a program in Jupyter Notebook
  • How to read a text file in Python
  • How to add numbers in Python
  • How to ask for user input in Python
  • How to debug in Python
  • How to create a thread in Python
  • How to end a program in Python
  • How to import a library in Python
  • How to use the PIP package manager
  • How to use classes in Python
  • How to reverse strings in Python
  • How to convert a string to int in Python
  • How to print on the same line in Python
  • How to remove items from a list
  • How to add to a dictionary in Python
  • How to raise an exception in Python
  • How to throw an exception in Python
  • How to stop a program in Python
  • How to use Python assert
  • How to use the Python compiler
  • How to use Python decorator
  • How to use the Python argparse module
  • How to use Python queue
  • How to use Python threading
  • How to concatenate strings in Python
  • How to use the Python absolute value
  • How to use the Python comparison operators
  • How to use the Python logical operators
  • How to use Python if-elif-else statements
  • How to use the Python printing method and indentation

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!

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

How do you indent print statements in python?

How do you indent a statement in Python?

It is preferred to use whitespaces instead of tabs to indent in python. Also, either use whitespace or tabs to indent; intermixing of tabs and whitespaces in indentation can cause wrong indentation errors.

Does Python use indents?

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.

Which key is used to indent statements in a Python program?

Answer: You can use the Tab key to indent a statement in python.