Python How-To: Prepending And Appending Text To Fill Space
Tags: python, beginners, webdev, tutorial
Published: 2023-05-08 12:05:00

- [Introduction](#introduction)
- [Prepend And / Or Append Text To Fill Space](#prepend-and--or-append-text-to-fill-space)
  - [Example One: Align Text To The Left And Fill After](#example-one-align-text-to-the-left-and-fill-after)
  - [Example Two: Align Text To The Right And Fill Before](#example-two-align-text-to-the-right-and-fill-before)
  - [Example Three: Align Text In The Centre And Fill Before And After](#example-three-align-text-in-the-centre-and-fill-before-and-after)
  - [Example Four: Creating A Terminal Welcome Screen](#example-four-creating-a-terminal-welcome-screen)
  - [Example Five: Creating A Terminal Welcome Screen With A Docstring](#example-five-creating-a-terminal-welcome-screen-with-a-docstring)
- [Conclusion](#conclusion)

## Introduction

When developing applications, there could come a time when the text output to a terminal needs to have some text prepended or appended to it. For example, outputting some text inside of a box to make it stand out.

Within Python, there are a number of methods for how to do this but in this article, I'll cover how to use the `ljust`, `rjust` and `center` methods to perform both text alignment and prepending and / or appending additional characters.

With that said, let's begin.

## Prepend And / Or Append Text To Fill Space

To begin, let's set some variables that will be used across the first three examples:

``` python
output_length = 35
text = "Hello World"
fill_char = "*"
```

- output_length: Defines how long the text output should be. In this case, a maximum of 35 characters.
- text: What text to display. This will be slightly modified for each example to add spacing where needed.
- fill_char: For any empty characters in the output (35 - text), place a `*`.

Now, let's take a look at some examples.

### Example One: Align Text To The Left And Fill After

For the first example, the text will be aligned to the left with one space after it.

``` python
text_left = f"{text} "
print(f"{text_left.ljust(output_length, fill_char)}")
```

Output:

![001](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgncuojbm5zu9vbxqxxb.png)

Ok, let's go over the code to understand how this works:

- text_left = f"{text} ": This is a new variable that take the previously defined `text` variable and adds a space to the end.
- print(f"{text_left.ljust(output_length, fill_char)}"): A standard `print` f-string that uses the `text_left` variable for its text. The text then uses the `ljust` method to indicate the text should be justified to the left. It then has both the `output_length` and `fill_char` variables to specify the overall length of the output and which character to use (`*`) to fill out any space that is blank after the text.

### Example Two: Align Text To The Right And Fill Before

In this example, the text will be aligned to the right with one space before it.

``` python
text_right = f" {text}"
print(f"{text_right.rjust(output_length, fill_char)}")
```

`rjust` works the same as `ljust` but with the text being on the right instead.

Output:

![002](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1np5zhipheol2zrod1v0.png)

### Example Three: Align Text In The Centre And Fill Before And After

In this example, the text will be aligned in the centre with one space before and after it.

``` python
text_centre = f" {text} "
print(f"{text_centre.center(output_length, fill_char)}")
```

Output:

![003](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpu5nsns7r7squfskskg.png)

Again, this works similar to the previous two examples with the text being in the centre of the `output_length`. Any blank space will be filled evenly (as best possible) on either side of the text.

### Example Four: Creating A Terminal Welcome Screen

This example will be a little be more involved but overall, it is a simple example of how to use the `ljust`, `rjust` and `center` methods together.

``` python
output_length = 35
fill_char = " "
border_char = "*"
header_footer_row = f"{border_char * output_length}"
empty_row = f"{border_char.ljust(output_length - 1, fill_char)}{border_char}"
text_row_one = "Welcome to my program"
text_row_two = "=" * len(text_row_one)


print(f"\033[1;32;40m{header_footer_row}")
print(empty_row)
print(empty_row)

print(f"\033[1;32;40m{border_char}\033[1;35;40m\
{text_row_one.center(output_length - 2, fill_char)}\033[1;32;40m\
{border_char}")

print(f"\033[1;32;40m{border_char}\033[1;36;40m\
{text_row_two.center(output_length - 2, fill_char)}\033[1;32;40m\
{border_char}")

print(empty_row)
print(empty_row)
print(header_footer_row)
```

Output:

![004](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4lgjwiyjet02hvqkdzzz.png)

To explain the code a little more, most of it was similar to the previous examples with some additional variables added in:

- header_footer_row: This is the header and footer of the box. It is the border_char (`*`) timesed by the output_length (35) to give a row of 35 `*`'s.
- empty_row: A row that is used for spacing. It starts with a `*` then has empty spaces (`fill_char`) and then one final `*`.
- text_row_one: The main text to show in the box.
- text_row_two: A row of equal signs that matches the length of the text in text_row_one.

Once the variables have been defined, the rest of the code is a collection of print statements that displays the box with the two `text_rows` in the middle.

The box is coloured in green (`\033[1;32;40m`), with the text being magenta (`\033[1;35;40m`) and the equals signs being cyan (`\033[1;36;40m`).

For more information on how to add colour to the terminal output, please see [this post]([http://link_to_page](https://dev.to/dev_neil_a/python-how-to-adding-color-and-styles-to-terminal-text-3699)) which covers two methods to colour the terminal output.

### Example Five: Creating A Terminal Welcome Screen With A Docstring

The final example is a repeat of example four but instead of using multiple print functions, it uses a docstring inside of a single print function. One difference is the `text_rows` have different text.

``` python
output_length = 35
fill_char = " "
border_char = "*"
header_footer_row = f"{border_char * output_length}"
empty_row = f"{border_char.ljust(output_length - 1, ' ')}{border_char}"
text_row_one = "Welcome to my other program"
text_row_two = "=" * len(text_row_one)


print(f"""\033[1;32;40m{header_footer_row}
{empty_row}
{empty_row}
\033[1;32;40m{border_char}\033[1;35;40m\
{text_row_one.center(output_length - 2, fill_char)}\033[1;32;40m\
{border_char}
\033[1;32;40m{border_char}\033[1;36;40m\
{text_row_two.center(output_length - 2, fill_char)}\033[1;32;40m\
{border_char}
{empty_row}
{empty_row}
\033[1;32;40m{header_footer_row}""")
```

Output:

![005](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iprlkdckopvvqnrpeep.png)

## Conclusion

I hope that this article was useful. As mentioned in the introduction, there are other methods, such as the `format` method that can be used. Please feel free to explore that and any other methods that are available.

Thank you for reading and have a nice day!