making a string a given length?

Doug Stanfield DOUGS at oceanic.com
Sat May 27 22:49:12 EDT 2000


[Z 3 Penguin asked:]
> i need to 
> make a string
> (like +ACI-abc+ACI-) a given number of bytes long.

[to which Emile van Sebille replied:]
> 
> Or, in 1.6:
> 
> >>> "hello".rjust(20)
> '               hello'
> >>> "hello".ljust(20)
> 'hello               '
> 

To be complete, an example from what I wrote yesterday:

line = string.center(string.join(spam,", "),61)[0:61]

These three functions of the string module don't truncate, so if you want it
no longer than "a given number of bytes" use the slice.

-Doug-




More information about the Python-list mailing list