padding strings

Paul Prescod paul at prescod.net
Thu Apr 29 15:00:33 EDT 1999


Roy Smith wrote:
> 
> Given a string, I want to generate another string which is exactly N
> characters long.  If the first string is less than N, I want to blank-pad
> it.  If the first string is greater than N, I want to truncate it.
> 
> What's the most straight-forward way to do that?

How about this:

def mypad( s, num ):
    return string.ljust( s, num )[:num]

-- 
 Paul Prescod  - ISOGEN Consulting Engineer speaking for only himself
 http://itrc.uwaterloo.ca/~papresco

"Microsoft spokesman Ian Hatton admits that the Linux system would have
performed better had it been tuned."
"Future press releases on the issue will clearly state that the research
was sponsored by Microsoft."
  http://www.itweb.co.za/sections/enterprise/1999/9904221410.asp




More information about the Python-list mailing list