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 โ€บ 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 - ... a = "hello" # Pads to the right, making it 10 characters long result = "{:<10}".format(a) print(result) ... "{:<10}" specifies that the string should be left-aligned and padded with spaces to make the total length 10 characters.
Discussions

python - How can I pad a string with spaces from the right and left? - Stack Overflow
I have two scenarios where I need to pad a string with whitespaces up to a certain length, in both the left and right directions (in separate cases). For instance, I have the string: TEST but I ne... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Allow f-string to dynamically pad spaces
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 ... More on discuss.python.org
๐ŸŒ discuss.python.org
2
0
June 27, 2023
Python 3, best way to pad/remove spaces in between strings?
Format strings can do that for you. https://docs.python.org/3.7/library/string.html#format-string-syntax name = 'John' occupation = 'Plumber' output = '{:<15}{}'.format(name, occupation) output 'John Plumber' More on reddit.com
๐ŸŒ r/learnpython
9
5
October 1, 2018
String Format Padding
You want to pad with zeros instead of spaces (default behavior). The documentation is totally not clear about it. Best too google stack overflow for these types of questions "python pad leading zero". >>> # pad with 0 so width is 2 >>> print "%02d" % 1 01 >>> # pad with 0 so width is 3 >>> print "%03d" % 5 005 More on reddit.com
๐ŸŒ r/learnpython
2
1
December 12, 2014
๐ŸŒ
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
๐ŸŒ
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 - To pad the string with desired ... to the right of the given string or pad them. The rjust() function is used to fill out the spaces to the left of the given string or pad them....
๐ŸŒ
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 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ add-padding-to-a-string-in-python
Add padding to a string in Python - GeeksforGeeks
July 23, 2025 - Let's see some more methods on how we can add padding to a string in Python. ... Pythonโ€™s built-in string methods such as str.ljust(), str.rjust() and str.center() offer the flexibility to use custom characters beyond spaces, making them highly versatile for various use cases. ... s = "Python" # Left-align with dashes left_padded = s.ljust(10, "-") # Right-align with dashes right_padded = s.rjust(10, "-") # Center-align with dashes center_padded = s.center(10, "-") print(left_padded) print(right_padded) print(center_padded)
Find elsewhere
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ padding-python
Python - String Padding Examples: ljust, rjust - Dot Net Perls
# ... First char in format string is a space. value = format("line 1", " >10s") print(value) # Left-align a number in 10 chars. # ... Pad with a star character. print(format( ... Consider thisโ€”a loop could be used to pad strings. This can be needed for more complex requirements.
๐ŸŒ
BTech Geeks
btechgeeks.com โ€บ home โ€บ pad string with spaces python โ€“ python: how to pad strings with zero, space or some other character?
Pad string with spaces python - Python: How to pad strings with zero, space or some other character? - BTech Geeks
September 20, 2024 - numStr = "6" print('Original String :', numStr) # Make string right justified of length 4 by padding 3 spaces to left numStr = numStr.rjust(4, ' ') print('Updated String :', numStr) ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ pad-strings-with-spaces-in-python
How To Pad Strings With Spaces In Python?
March 19, 2025 - ... I have executed the above example code and added the screenshot below. "{:<10}" means: < โ†’ Left-align the text. 10 โ†’ The total width of the string (including the original text and padding).
๐ŸŒ
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?
You can adjust the width parameter to control how much padding you want, and you can specify a different fill character if you don't want to use spaces. For example, you can use str.ljust(10, '-') to pad with hyphens instead of spaces. "Python pad string with spaces from left and right"
๐ŸŒ
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()
๐ŸŒ
Medium
medium.com โ€บ @johnidouglasmarangon โ€บ padding-f-strings-in-python-977b17edbd36
Padding f-strings in Python
February 18, 2022 - This is an example of format typing for: decimal, octal, hexadecimal (capitalized) and binary values. number = 2022 padding = 10print(f"{'decimal':<{padding}}{number:d}") print(f"{'octal':<{padding}}{number:o}") print(f"{'hex':<{padding}}{n...
๐ŸŒ
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.
๐ŸŒ
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 of 10 characters is specified using the > character. The value will be right-aligned within a field of width 10 by using:>10. ... There will be 7 spaces before the value โ€œGoomโ€ because it will be right-aligned within a field that is 10 pixels wide.
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ how-to-pad-a-string-to-a-fixed-length-with-spaces-in-python.html
How to pad a string to a fixed length with spaces in python
Description: Using the ljust() method, you can append spaces to the end of a string to reach a specific length. original_string = "example" padded_string = original_string.ljust(20) # Output: "example " "Python pad string to fixed length with leading spaces"
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ how can i fill out a python string with spaces?
How can I fill out a Python string with spaces? | Codemia
September 23, 2025 - For formatted output, especially inside f-strings, width specifiers are often cleaner. ... 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. Here is a practical example using space padding to keep columns aligned. ... 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}")