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
🌐
W3Schools
w3schools.com › python › ref_string_zfill.asp
Python String zfill() Method
Python Examples Python Compiler ... Python Interview Q&A Python Training ... The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length....
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pad Strings and Numbers with Zeros in Python (Zero-padding) | note.nkmk.me
May 18, 2023 - Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility. Built-in Types - printf-style String Formatting — Python 3.11.3 documentation ... i = 1234 print('Zero Padding: d' % i) # Zero Padding: 00001234 i = -1234 print('Zero Padding: d' % i) # Zero Padding: -0001234 ... For strings, it is filled with spaces.
🌐
Stack Abuse
stackabuse.com › padding-strings-in-python
Padding Strings in Python
September 18, 2023 - To combat this, we could write for/while loops and append spaces to the strings until they reached a certain length, and make sure that all the data is aligned properly for easy visual inspection. Or, we could use built-in functions that can achieve the same goal. The functions we'll be taking a look at in this article are: ljust(), center(), rjust(), zfill() and format().
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › string › zfill › python-string-zfill
Python zfill() function | Why do we use Python String zfill() function? |
September 27, 2021 - # Python program to illustrate zfill() txt = 'Python programming' print('Original string:', txt) print('New string:', txt.zfill(25)) print('New string:', txt.zfill(20)) print('New string:', txt.zfill(18)) ... Original string: Python programming New string: 0000000Python programming New string: 00Python programming New string: Python programming ... We simply initialized a string named txt with the string value ‘Python programming’. The length of the string, in this case, is 18 (including the white space).
🌐
GeeksforGeeks
geeksforgeeks.org › 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
November 27, 2024 - The f"{a:<10}" f-string pads the string a to the right with spaces until it reaches 10 characters. You can change the alignment by using {:>10} for padding to the left. ... Padding a string to a fixed length with zeros in Python is basically ...
🌐
i2tutorials
i2tutorials.com › home › zfill() method in python string module
zfill() method in Python String Module | i2tutorials
May 16, 2020 - The zfill counts spaces as well like it counts characters or values. Syntax: str.zfill() Example-1: txt = "Quality is most important" x = txt.zfill(40) print(x) Output : Python Tutorial ·
🌐
Runebook.dev
runebook.dev › en › docs › python › library › stdtypes › str.zfill
python - The str.zfill() Method: Best Practices, Common Errors, and the f-String Way
The most frequent "trouble" is realizing that zfill() only pads with the character '0'. If you need to pad with spaces, asterisks, or any other character, zfill() isn't the right tool.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to fill a python string with spaces?
How to Fill a Python String with Spaces? - Be on the Right Side of Change
July 10, 2021 - A simple way without f-strings to fill a given string with empty spaces to obtain a left-justified string is to use string concatenation arithmetic via the overloaded + and * operators.
Find elsewhere
🌐
DataScience Made Simple
datasciencemadesimple.com › home › zfill() function in python
zfill() Function in Python - DataScience Made Simple
September 22, 2020 - Format function takes up the numeric value as argument and pads them with preceding zeros until the desired length is reached.
🌐
Initial Commit
initialcommit.com › blog › python-zfill-method
Python zfill Method
November 1, 2021 - The first argument is the same ... padding with any character. If you don't specify the second argument rjust() just does padding with the space character....
🌐
Python Pool
pythonpool.com › home › tutorials › python zfill(): zero-pad strings, numbers, and signs
Python zfill(): Zero-Pad Strings, Numbers, and Signs
July 13, 2026 - ... This behavior is different from blindly adding zeros to the left. It keeps signed numeric strings readable and matches what most people expect in reports. If the string contains spaces before the sign, the spaces count as normal characters.
🌐
datagy
datagy.io › home › python posts › python strings › python zfill & rjust: pad a string in python
Python zfill & rjust: Pad a String in Python • datagy
December 16, 2022 - In the following two sections you’ll ... to the zfill method! There may be times that you want to pad a string but you don’t want to have it filled with zero values. In these cases, it can be helpful to use the rjust string method. The rjust method is applied to a string and takes two parameters: (1) the width of the resulting string and (2) the character to use to pad the original string. Let’s use a space to pad our string using the rjust method: # Using .rjust to Pad a Python String a_string ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › fill-a-python-string-with-spaces
Fill a Python String with Spaces - GeeksforGeeks
July 23, 2025 - Note that in the case of space around the string if N is odd then the extra space is added to the right of the string. Input: string = "GeeksforGeeks" N = 2 Output: Right: "GeeksforGeeks " Left: " GeeksforGeeks" Around: " GeeksforGeeks " Input: string = "Geek" N = 5 Output: Right: "Geek " Left: " Geek" Around: " Geek " For more effective handling of sophisticated string formatting, the Python string format() method has been developed.
🌐
Python Engineer
python-engineer.com › posts › pad-zeros-string
How to pad zeros to a String in Python - Python Engineer
It returns a copy of the object right justified in a sequence of length width. The default filling character is a space. However, this does not handle a sign prefix. >>> "42".rjust(5) ' 42' >>> "42".rjust(5, "0") '00042' >>> "-42".rjust(5, "0") ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python pad string with zeros
Python Pad String With Zeros - Spark By {Examples}
May 21, 2024 - How to pad string with zeros in Python? Adding some characters/values to the string is known as Padding. In Python, zfill() and rjust() methods are used
🌐
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 characters python provides there methods namely, ljust(), rjust() and center(). The ljust() function is used to fill out the spaces to the right of the given string or pad them.
🌐
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
🌐
iO Flood
ioflood.com › blog › python-zfill
Python's 'zfill()' Method | Guide To Padding Numeric Strings
February 14, 2024 - In this example, we have a string ‘7’ that we want to pad with zeros until it’s three characters long. We use the zfill method with the argument 3, and it returns ‘007’, which is exactly what we want. This is just a quick introduction to Python’s zfill method.