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)
NumPy
numpy.org › doc › stable › reference › generated › numpy.pad.html
numpy.pad — NumPy v2.5 Manual
If a dict, each key is an axis and its corresponding value is an int or int pair describing the padding (before, after) or pad width for that axis.
03:50
Python Zero-Padding Magic: Format Numbers Like a Pro - YouTube
00:36
Padding Strings In Python | Python Tutorial - YouTube
00:46
Python String Formatting with F-Strings: Aligning Text and Padding ...
05:25
Padding in Numpy Array | Numpy functions | Python Numpy Tutorial ...
Top answer 1 of 14
1045
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
648
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!'
Stack Abuse
stackabuse.com › padding-strings-in-python
Padding Strings in Python
September 18, 2023 - We love Python. Right Padding: Use < inside placeholders to left align a string:
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.pad.html
pandas.Series.str.pad — pandas 3.0.6 documentation - PyData |
Pad strings in the Series/Index up to width.
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.
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.
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-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.
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))