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 OverflowGeeksforGeeks
geeksforgeeks.org โบ python โบ add-padding-to-a-string-in-python
Add padding to a string in Python - GeeksforGeeks
July 23, 2025 - We can manually add padding by calculating the required number of characters and concatenating them to the string. ... s = "Python" width = 10 # Add padding padded = " " * (width - len(s)) + s # Right-align print(padded)
Top answer 1 of 14
1040
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 '
2 of 14
645
For a flexible method that works even when formatting complicated string, you probably should use the string-formatting mini-language,
using either f-strings
>>> f'{"Hi": <16} StackOverflow!' # Python >= 3.6
'Hi StackOverflow!'
or the str.format() method
>>> '{0: <16} StackOverflow!'.format('Hi') # Python >=2.6
'Hi StackOverflow!'
Videos
00:36
Padding Strings In Python | Python Tutorial - YouTube
02:39
Python Basics 02 - 06/08 Padding for Strings - YouTube
09:02
Python standard library: String padding - YouTube
Python String Padding Trick You Must Know #python #shorts ...
How to pad zeroes to a string in Python
03:33
python f string padding - YouTube
Stack Abuse
stackabuse.com โบ padding-strings-in-python
Padding Strings in Python
September 18, 2023 - Right padding is analogous to left padding - the given character is added to the end of the string until the string reaches a certain length. Python offers many functions to format and handle strings, their use depends on the use case and the developer's personal preference.
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()
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.
Codeguage
codeguage.com โบ v1 โบ courses โบ python โบ strings-string-padding
Mini courses - Learn in a week! | Codeguage
How a string of bits represents different kinds of data.
Dot Net Perls
dotnetperls.com โบ padding-python
Python - String Padding Examples: ljust, rjust - Dot Net Perls
To pad strings in Python, we can use the format built-in or directly use ljust and rjust.
Delft Stack
delftstack.com โบ home โบ howto โบ python โบ python pad string with spaces
How to Pad String With Spaces in Python | Delft Stack
February 2, 2024 - In this code, we use Pythonโs string formatting method. We create a format string "{:15}" where the : indicates a placeholder for the string, and 15 specifies the desired width. We apply the format method to insert "I am legend" into this placeholder. When executed, the code pads the string with spaces on the right side, ensuring the output is 15 characters wide.
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.
GeeksforGeeks
geeksforgeeks.org โบ python โบ pad-or-fill-a-string-by-a-variable-in-python-using-f-string
Pad or fill a string by a variable in Python using f-string - GeeksforGeeks
July 15, 2025 - We can pad strings to the left, right, or centre by using f-strings with a variable as the padding character.
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 - The simplest way to pad a string with zeros is by using Pythonโs built-in zfill() method.
CodeRivers
coderivers.org โบ blog โบ python-pad-string
Python Pad String: A Comprehensive Guide - CodeRivers
February 22, 2026 - In Python, string padding is a useful operation that allows you to format strings in a specific way. Padding can be used to align text, create fixed-width fields, or add decorative elements to strings. This blog post will explore the fundamental concepts of Python pad string, different usage ...
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 # pad spaces in string # upto fixed length # initialising string ini_string = "123abcjw" padding_size = 15 # printing string and its length print ("initial string : ", 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))