pad

How can I fill out a Python string with spaces?

时光怂恿深爱的人放手 提交于 2019-11-25 23:16:12
问题 I want to fill out a string with spaces. I know that the following works for zero\'s: >>> print \"\'%06d\'\"%4 \'000004\' But what should I do when I want this?: \'hi \' of course I can measure string length and do str+\" \"*leftover , but I\'d like the shortest way. 回答1: 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