I'd like to add a space on the right according to the length when printing.

Asked 2 years ago, Updated 2 years ago, 128 views

To fill in the first 0:

>>> print  "'%06d'"%4
'000004'

This is what I know

'hi    '

How do I add spaces back together? str+""*number I know we can write together.

string python string-formatting

2022-09-22 22:12

1 Answers

There is a way to write str.ljust(width[, fillchar]).

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

Returns a string of left-aligned width lengths. It is padded with fillchar, and the default value of fillchar is one space.

>>> 'hi'.ljust(10)
'hi        '


2022-09-22 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.