How can I fill out a Python string with spaces?

PythonServer Side ProgrammingProgramming

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$$$
raja
Published on 11-Dec-2017 15:13:57
Advertisements