Python
string manipulation
formatting
spaces
programming tips

How can I fill out a Python string with spaces?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you want to "fill out" a Python string with spaces, you are usually talking about padding it to a fixed width. Python gives you built-in methods for left, right, and center alignment, plus format-specifier support for more structured output. The right choice depends on whether you are formatting plain text, tables, or numeric values.

Pad with ljust, rjust, and center

The simplest tools are the string methods ljust, rjust, and center. They all return a new string padded to the requested width.

python
1text = "cat"
2
3print(repr(text.ljust(8)))
4print(repr(text.rjust(8)))
5print(repr(text.center(8)))

Output:

text
'cat     '
'     cat'
'  cat   '

These methods pad with spaces by default, which is exactly what you want in most alignment tasks.

Using Format Specifiers

For formatted output, especially inside f-strings, width specifiers are often cleaner.

python
1name = "Ada"
2score = 98
3
4print(f"|{name:<10}|{score:>5}|")
5print(f"|{'title':^10}|")

The alignment symbols are:

  • '< for left alignment'
  • '> for right alignment'
  • '^ for centered text'

This style is very convenient when you are printing reports or console tables because the width and alignment are easy to see at a glance.

Building a Text Table

Here is a practical example using space padding to keep columns aligned.

python
1rows = [
2    ("Ada", "Python"),
3    ("Grace", "COBOL"),
4    ("Linus", "C"),
5]
6
7print(f"{'Name':<10} {'Language':<10}")
8print(f"{'-' * 10} {'-' * 10}")
9
10for name, language in rows:
11    print(f"{name:<10} {language:<10}")

Output:

text
1Name       Language
2---------- ----------
3Ada        Python
4Grace      COBOL
5Linus      C

This is the most common real-world use case for padding with spaces.

Manual Padding with Multiplication

If you need full control, you can append spaces manually.

python
1text = "cat"
2width = 8
3
4padded = text + " " * (width - len(text))
5print(repr(padded))

This approach is easy to understand, but it is more error-prone than ljust because you must handle short widths yourself. For example, if width is less than len(text), the multiplication becomes zero-length and the string is returned unchanged.

In most cases, the built-in methods are clearer and safer.

Padding on the Left or Both Sides

You can do the same kind of manual work on the left or on both sides, but again the built-in methods usually read better:

python
1text = "42"
2
3print(repr(text.rjust(6)))
4print(repr(text.center(6)))

If you are formatting numbers, note that zfill is a separate tool for zero-padding rather than space-padding:

python
print("42".zfill(6))

That produces 000042, which is useful for numeric identifiers but not for general text alignment.

Strings Are Immutable

An important detail is that these methods do not modify the original string. They return a new one.

python
1text = "cat"
2text.ljust(8)
3
4print(repr(text))

The output is still:

text
'cat'

So if you need the padded result later, assign it to a variable or print it directly.

Common Pitfalls

One common mistake is expecting ljust, rjust, or center to change the string in place. Python strings are immutable, so you must use the returned value.

Another issue is confusing display width with character count. Unicode characters can take different visual widths depending on the terminal or font, so simple padding may not always line up perfectly in every environment.

Developers also sometimes use manual string concatenation where an f-string with width specifiers would be much easier to read and maintain.

Finally, remember that zfill is not a general padding method. It is specifically for zero-padding and is usually intended for numeric-looking strings.

Summary

  • Use ljust, rjust, and center for simple space padding.
  • Use width specifiers in f-strings for clean, table-like formatting.
  • Manual padding with " " * n works, but the built-in methods are usually clearer.
  • Strings are immutable, so store or print the returned padded value.
  • Watch for Unicode display-width issues when perfect visual alignment matters.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.