padding strings

jkraai at murl.com jkraai at murl.com
Thu Apr 29 15:31:00 EDT 1999


padchar = ' '
N = 50

str = 'I need to be padded'
padded = (str+padchar*N)[:N]

print '>'+padded+'<'
print 'len:',len(padded)

--jim

-----Original Message-----
From: Roy Smith <roy at endeavor.med.nyu.edu>
Newsgroups: comp.lang.python
To: python-list at cwi.nl <python-list at cwi.nl>
Date: Thursday, April 29, 1999 2:01 PM
Subject: padding strings


>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?
>
>-- 
>Roy Smith <roy at popmail.med.nyu.edu>
>New York University School of Medicine
>





More information about the Python-list mailing list