You can do this with str.ljust(width[, fillchar]):

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s).

>>> 'hi'.ljust(10)
'hi        '
Answer from Felix Kling on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ add-padding-to-a-string-in-python
Add padding to a string in Python - GeeksforGeeks
July 23, 2025 - s = "Python" width = 10 # Add padding padded = " " * (width - len(s)) + s # Right-align print(padded) ... The " " * (width - len(s)) creates the required padding by repeating the space character.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-pad-a-string-to-a-fixed-length-with-spaces-in-python
How to Pad a String to a Fixed Length with Spaces in Python - GeeksforGeeks
July 23, 2025 - The simplest and most common way to pad a string in Python is by using the built-in ljust() and rjust() methods. These methods add spaces to the left or right of the string until the string reaches a specific length.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ pad string with spaces in python
Pad String with Spaces in Python - Spark By {Examples}
May 21, 2024 - How to pad the string with spaces in Python? Adding some characters to the string is known as Padding or filling and is useful when you wanted to make a
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ padding-strings-in-python
Padding Strings in Python
September 18, 2023 - The ljust() function aligns a string to the left by adding right padding. The ljust() function takes two parameters: width and fillchar. The width is mandatory and specifies the length of the string after adding padding, while the second parameter is optional and represents the character added ...
๐ŸŒ
YouTube
youtube.com โ€บ codefast
python left pad string with spaces - YouTube
Download this code from https://codegive.com In this tutorial, we'll explore how to left-pad a string with spaces in Python. Left-padding is a common operati...
Published: December 13, 2023
Views: 5
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ padding-python
Python - String Padding Examples: ljust, rjust - Dot Net Perls
Spaces can be placed to the left or to the right of a Python string. We can left or right-justify a table of text data with paddingโ€”we use ljust and rjust.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @johnidouglasmarangon โ€บ padding-f-strings-in-python-977b17edbd36
Padding f-strings in Python
February 18, 2022 - number = 2022 padding = 10print(f"{'decimal':<{padding}}{number:d}") print(f"{'octal':<{padding}}{number:o}") print(f"{'hex':<{padding}}{number:X}") print(f"{'binary':<{padding}}{number:b}")>>> decimal 2022 >>> octal 3746 >>> hex 7E6 >>> binary 11111100110 ... This is a way to use a set of format strings that will be interpreted together. Take a look at the official documentation of mini-language specification. message = 'Hi' fill = '-' align = '^' width = 16print(f'{message:{fill}{align}{width}}')>>> -------Hi------- See the notebook with the examples shown in this post.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Allow f-string to dynamically pad spaces - Python Help - Discussions on Python.org
June 27, 2023 - Example: # This works >>> print(f"{3:4}") '3 ' # However this does not work. >>> pad_amount: int = ... >>> print(f"3:pad_amount") ValueError: Unknown format code 'a' for object of type 'int' # I know this is the intended behavior, however the only other way to do this (or if you have better ways to do this please let me know) is using eval()
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-i-fill-out-a-python-string-with-spaces
How can I fill out a Python string with spaces?
October 19, 2022 - The center() function is used to fill out the spaces to both left and right to the string or pad them otherwise.
๐ŸŒ
Python Guides
pythonguides.com โ€บ pad-strings-with-spaces-in-python
How To Pad Strings With Spaces In Python?
March 19, 2025 - Conversely, the rjust() method in Python pads a string with spaces on the left side.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.str.pad.html
pandas.Series.str.pad โ€” pandas 3.0.6 documentation - PyData |
This function pads strings in a Series or Index to a specified width, filling the extra space with a character of your choice. It provides flexibility in positioning the padding, allowing it to be added to the left, right, or both sides.
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ how-can-i-fill-out-a-python-string-with-spaces
How to Pad a Python String with Spaces (Shortest Method) โ€” pythontutorials.net
Center padding: Adding spaces both left and right to center the string (e.g., "hello" โ†’ " hello " to reach length 8). Padding ensures text is visually consistent, making it easier to read in outputs like tables, logs, or user interfaces. Python offers several built-in tools to pad strings with spaces.
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ how-to-pad-a-string-with-spaces-from-the-right-and-left-in-python.html
How to pad a string with spaces from the right and left in python?
Description: To pad a string with spaces from both the left and right sides in Python, you can use string formatting with the str.format() method.
๐ŸŒ
Medium
medium.com โ€บ pythons-gurus โ€บ how-padding-works-in-python-and-how-to-use-it-8c0020830f8d
How Padding Works in Python and How to Use It | Pythonโ€™s Gurus
December 5, 2024 - ... For numbers, padding is often achieved during formatting, especially when displaying output or logging data. The format() method can be used to pad numbers with zeros or spaces.
๐ŸŒ
CoenRaets
jsonviewer.ai โ€บ python-f-string-padding
Python f String Padding[Complete Guide with Examples] - JSON Viewer
July 5, 2023 - The character โ€œ>โ€ indicates right padding for the employee with a total width of 10, while the โ€œ<โ€ character indicates left padding for the id with the total width of 10. ... Here, the variable value is written after the string โ€œValue: โ€ The alignment of the value within a field ...