Computer >> Computer tutorials >  >> Programming >> Python

How can I fill out a Python string with spaces?


To pad the string on the right with spaces or any other character you can use the str.ljust(width[, fillchar]) method. The fillchar arg is used to specify the character to fill. For example:

Example

To pad the string on the left with spaces or any other character you can use the str.ljust(width[, fillchar]) method. The fillchar arg is used to specify the character to fill. For example:

print('Mona Lisa'.ljust(12))
print('Mona Lisa'.ljust(12,'$'))

Output

Mona Lisa  
Mona Lisa$$$