What is the simplest method to get a vector result?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jul 24 23:23:29 EDT 2014


On Thu, 24 Jul 2014 22:44:51 -0400, Dave Angel wrote:

> fl <rxjwg98 at gmail.com> Wrote in message:
> 
> 
>>  I am puzzled about the last part of your code and want to learn
>> from it ("   * " " + "*"   ").
>> 
>> 
> 6 * " " + "x"
> 
> will produce a string of 6 blanks followed by an x.


Dave is correct, but if you want to format text to a certain width, it is 
better to have Python count how many spaces you need:


py> 'spam'.rjust(20)
'                spam'
py> 'spam'.ljust(20)
'spam                '
py> 'spam'.center(20)
'        spam        '


In recent versions, you can even specify the fill character:

py> 'spam'.center(20, '.')
'........spam........'


-- 
Steven



More information about the Python-list mailing list