DEV Community

Cover image for Python How-To: Prepending And Appending Text To Fill Space
dev_neil_a
dev_neil_a

Posted on

Python How-To: Prepending And Appending Text To Fill Space

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:

output_length = 35
text = "Hello World"
fill_char = "*"
Enter fullscreen mode Exit fullscreen mode
  • 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.

text_left = f"{text} "
print(f"{text_left.ljust(output_length, fill_char)}")
Enter fullscreen mode Exit fullscreen mode

Output:

001

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.

text_right = f" {text}"
print(f"{text_right.rjust(output_length, fill_char)}")
Enter fullscreen mode Exit fullscreen mode

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

Output:

002

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.

text_centre = f" {text} "
print(f"{text_centre.center(output_length, fill_char)}")
Enter fullscreen mode Exit fullscreen mode

Output:

003

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.

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)
Enter fullscreen mode Exit fullscreen mode

Output:

004

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 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.

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}""")
Enter fullscreen mode Exit fullscreen mode

Output:

005

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!

Top comments (0)