Asked  7 Months ago    Answers:  5   Viewed   89 times

I want to fill out a string with spaces. I know that the following works for zero's:

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

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.



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        '
Tuesday, June 1, 2021
answered 7 Months ago

You'd use str.join() on the list without string formatting, then interpolate the result:

"Hello %s" % ', '.join(my_args)


>>> my_args = ["foo", "bar", "baz"]
>>> "Hello %s" % ', '.join(my_args)
'Hello foo, bar, baz'

If some of your arguments are not yet strings, use a list comprehension:

>>> my_args = ["foo", "bar", 42]
>>> "Hello %s" % ', '.join([str(e) for e in my_args])
'Hello foo, bar, 42'

or use map(str, ...):

>>> "Hello %s" % ', '.join(map(str, my_args))
'Hello foo, bar, 42'

You'd do the same with your function:

function_in_library("Hello %s", ', '.join(my_args))

If you are limited by a (rather arbitrary) restriction that you cannot use a join in the interpolation argument list, use a join to create the formatting string instead:

function_in_library("Hello %s" % ', '.join(['%s'] * len(my_args)), my_args)
Wednesday, August 4, 2021
answered 4 Months ago

You didn't say what should happen to your Index, so I'm assuming it's unimportant.

In [12]: df.index = df['value']

In [15]: df.reindex(np.arange(df.value.min(), df.value.max() + 1)).fillna(0)
       value  freq
9          9     1
10         0     0
11        11     1
12        12     4
13         0     0
14         0     0
15        15     2
Friday, August 6, 2021
answered 4 Months ago

Python 3.2 documentation said that, % will eventually go away.

Since str.format() is quite new, a lot of Python code still uses the % operator. However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used.

But as @regilero says, the sentence is gone from 3.3, which might suggest it's not actually the case. There are some conversations here that suggest the same thing.

As of Python 3.4 the paragraph 7.1.1 reads:

The % operator can also be used for string formatting. It interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation.

See also Python 3.4 4.7.2 printf-style String Formatting.

Monday, September 13, 2021
answered 3 Months ago

I don't like option 2 as it introduces an unnecessary special case.

I would refactor out the construction of the prompt suffix:

    # Possible at top of program
    my $suffix =  ( ' ' x $p ) . $prompt;

    # Later...

    $key .= $suffix ;
Friday, October 29, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :