[Patches] [Patch #100913] Optional pad character (or string) for rjust, ljust

noreply@sourceforge.net noreply@sourceforge.net
Mon, 31 Jul 2000 02:49:05 -0700


Patch #100913 has been updated. 

Project: 
Category: None
Status: Open
Summary: Optional pad character (or string) for rjust, ljust

Follow-Ups:

Date: 2000-Jul-17 04:45
By: effbot

Comment:
looks fine to me, but you should really change the
unicode string type too (unicodeobject.c)
-------------------------------------------------------

Date: 2000-Jul-17 04:55
By: twouters

Comment:
And don't forget Lib/UserString.py ! :-)
+1 on the patch, though. I always had a nagging feeling this needed fixing, but I've never used it myself, and I thought there was another way to go 'bout it.

-------------------------------------------------------

Date: 2000-Jul-25 07:45
By: jhylton

Comment:
Can you also add code to the test suite to exercise this feature? Look in Lib/test/string_tests.py

-------------------------------------------------------

Date: 2000-Jul-26 23:48
By: moshez

Comment:
The semantics for strings are a bit confusing, and I'm not sure it's worth the complexity. Also, doing it once for strings and once for unicode is a bit redundant, when you can do it in pure Python for both types with ease...wouldn't it be better to to have ljust and rjust (with optional padding) remain in the string module, and make them work on both regular and unicode strings by polymorphism.

def rjust(s, l, pad=' '):
    missing = l-len(s)
    if missing > 0:
        return s+(pad*missing)
    else:
        return s

def ljust(s, l, pad=' '):
    missing = l-len(s)
    if missing > 0:
        return (pad*missing)+s
    else:
        return s

(Or the other way around, could never tell the difference between left and right)

-------------------------------------------------------

Date: 2000-Jul-31 02:49
By: nowonder

Comment:
It would be nice to also have the documentation patched.

BTW: I like the semantics for strings, but the pad=='' case is a bit strange:

"hello".rjust(10,'') gives "hello" (as far as I can see).
I would expect "hello".rjust(10,somestring) to return a string of length 10 regardless of the value of somestring.
On the other hand an empty string could be used to switch off padding. But that's a bit too magic for me. Maybe this should raise a ValueError?
-------------------------------------------------------

-------------------------------------------------------
For more info, visit:

http://sourceforge.net/patch/?func=detailpatch&patch_id=100913&group_id=5470