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
๐ŸŒ
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()
Discussions

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
How do I store user input (with spaces) in a string?
>> stops at whitespace. It needs some way to seperate inputs after all. The easiest way is to use std::getline instead: std::getline( std::cin, userinput ); More on reddit.com
๐ŸŒ r/cpp_questions
8
9
August 11, 2023
f-string and string right padding is driving me crazy
Your code works for me. >>> header = "foo" >>> endpoint = "bar" >>> ename = f'"{endpoint}"' >>> print(f'{header} {ename:<30}!') foo "bar"                         ! More on reddit.com
๐ŸŒ r/learnpython
10
10
January 11, 2025
how to remove leading space in formatting?
It seems like you're having trouble with formatted WRITE statements. I think what you probably want is WRITE(*,'(I0)') aa which uses the I0 format to output an integer without any padding. More traditionally in Fortran, you needed to specify the number of columns for an integer to be output (e.g., I10). For strings, you just write A as WRITE(*,'(A)') a and if your string is padded with spaces, you should usually use WRITE(*,'(A)') TRIM(ADJUSTL(a)). This will write the string without padding. In general, you're specifying the width of the output as 10 so you're going to get an output of width 10. Here is a pretty good reference with more info. More on reddit.com
๐ŸŒ r/fortran
1
4
August 31, 2022
๐ŸŒ
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. This padding is concatenated with the string ...
๐ŸŒ
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 center() method in Python pads a string to the specified length by adding spaces (or any other character) to both sides of the string. This method is particularly useful when you need to center-align text or data.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ fill-a-python-string-with-spaces
Fill a Python String with Spaces - GeeksforGeeks
July 23, 2025 - For filling space in Both left and right: string.center(space) where, space is the space given to the left, right or center padding + length of the string.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ padding-strings-in-python
Padding Strings in Python
September 18, 2023 - The functions we'll be taking a look at in this article are: ljust(), center(), rjust(), zfill() and format(). Any of these functions can be used to add a certain number of characters to either end of strings, including spaces. Before we take a closer look at the functions mentioned above, ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ 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.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.str.pad.html
pandas.Series.str.pad โ€” pandas 3.0.6 documentation
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-formatting-in-python
String Formatting in Python - GeeksforGeeks
The center() method is a built-in method in Python's str class that returns a new string that is centered within a string of a specified width. This code returns a new string padded with spaces on the left and right sides.
Published: March 18, 2026
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-pad-a-string-to-a-fixed-length-with-zeros-in-python
How to Pad a String to a Fixed Length with Zeros in Python - GeeksforGeeks
July 23, 2025 - When we want more control over how the string is padded, we can use the str.format() method. This method allows us to specify the total length and add zeros to the left. ... Using Python 3.6 or newer, f-strings provide a very clean and easy way to pad strings.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-get-a-string-left-padded-with-zeros-in-python
How to get a string left padded with zeros in Python?
March 24, 2026 - Python provides several built-in methods to left-pad strings with zeros: rjust(), format(), zfill(), and f-string formatting. The rjust() method right-justifies a string by padding it with a specified character on the left.
๐ŸŒ
Kodeclik
kodeclik.com โ€บ python-pad-string
How to add padding to a Python string
October 16, 2024 - Usually the padding character is a space but here we use another character just so that it is easy to observe the padding in action. name = "Kodeclik" print(name) newname = name.ljust(12,"-") print(newname) In the above code, we are left justifying the string โ€œnameโ€ with a length of 12, so four characters will be added as padding.
๐ŸŒ
Peterbe.com
peterbe.com โ€บ plog โ€บ zfill-string-in-python-with-variable
How to string pad a string in Python with a variable - Peterbe.com
October 19, 2021 - Well, the way you need to do it with f-string formatting, when it's a variable like that is this syntax: ... I think if its really only padding and you don't need any other f-string features: The built-in string method is the way to go! The f-string stuff looks like a voodoo meta language :/ Sadly I also taught myself some of the %-notation magic.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-padding-a-string-upto-fixed-length
Python | Padding a string upto fixed length - GeeksforGeeks
March 23, 2023 - # Python code to demonstrate # ... : ", ini_string, len(ini_string)) # code to pad spaces in string res = ini_string + " "*(padding_size - len(ini_string)) # printing string and its length print ("final string : ", res, len(res)) ...
๐ŸŒ
W3Schools
w3schools.com โ€บ cssref โ€บ css_selectors.php
CSS Selectors Reference
CSS selectors are used to "find" (or select) the HTML elements you want to style.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_compiler.asp
Python Online Compiler (Editor / Interpreter)
With our online Python compiler, you can edit Python code, and view the result in your browser. print("Hello, World!") x = "Python" y = "is" z = "awesome" print(x, y, z) ... Click on the "Try it Yourself" button to see how it works. If you want to create your own Python applications, W3Schools Spaces gives you an easy place to write, run, manage, and publish code online.
๐ŸŒ
Consortium for Street Children
streetchildren.org โ€บ home
Global Network Defending Street Children's Rights - CSC
February 27, 2026 - 21, one thing is clear: while progress has been made, we still have a long way to go in realising genuine, meaningful participation for street-connected children. Too often, their voices remain unheard in the very spaces where decisions about their lives are made.