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.
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.
Output:
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.
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.
Output:
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.
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:
If you are formatting numbers, note that zfill is a separate tool for zero-padding rather than space-padding:
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.
The output is still:
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, andcenterfor simple space padding. - Use width specifiers in f-strings for clean, table-like formatting.
- Manual padding with
" " * nworks, 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
- How can I filter a date of a DateTimeField in Django?
- How can I filter a Django query with a list of values?
- How can I filter tf.data.Dataset by specific values?
- How can I find all matches to a regular expression in Python?
- How can I find script's directory?
- How can I find the current OS in Python?
- How can I find the first occurrence of a sub-string in a python string?
- How can I find the index for a given item in a list?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.