diff options
author | Raymond Hettinger <python@rcn.com> | 2003-11-26 08:21:35 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-11-26 08:21:35 (GMT) |
commit | 4f8f9765766a126ebfff3c81655454821f1ad532 (patch) | |
tree | 90f6c08e42a551b8bb461d191afddaef5e4f0cf4 /Lib | |
parent | bd93b3ea8fd87742dd56c9c1ff514223892e0d4a (diff) | |
download | cpython-4f8f9765766a126ebfff3c81655454821f1ad532.zip cpython-4f8f9765766a126ebfff3c81655454821f1ad532.tar.gz cpython-4f8f9765766a126ebfff3c81655454821f1ad532.tar.bz2 |
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/UserString.py | 9 | ||||
-rw-r--r-- | Lib/string.py | 24 | ||||
-rw-r--r-- | Lib/test/string_tests.py | 6 |
3 files changed, 21 insertions, 18 deletions
diff --git a/Lib/UserString.py b/Lib/UserString.py index 730cdba..f28e54c 100755 --- a/Lib/UserString.py +++ b/Lib/UserString.py @@ -60,7 +60,8 @@ class UserString: # the following methods are defined in alphabetical order: def capitalize(self): return self.__class__(self.data.capitalize()) - def center(self, width): return self.__class__(self.data.center(width)) + def center(self, width, *args): + return self.__class__(self.data.center(width, *args)) def count(self, sub, start=0, end=sys.maxint): return self.data.count(sub, start, end) def decode(self, encoding=None, errors=None): # XXX improve this? @@ -97,7 +98,8 @@ class UserString: def istitle(self): return self.data.istitle() def isupper(self): return self.data.isupper() def join(self, seq): return self.data.join(seq) - def ljust(self, width): return self.__class__(self.data.ljust(width)) + def ljust(self, width, *args): + return self.__class__(self.data.ljust(width, *args)) def lower(self): return self.__class__(self.data.lower()) def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars)) def replace(self, old, new, maxsplit=-1): @@ -106,7 +108,8 @@ class UserString: return self.data.rfind(sub, start, end) def rindex(self, sub, start=0, end=sys.maxint): return self.data.rindex(sub, start, end) - def rjust(self, width): return self.__class__(self.data.rjust(width)) + def rjust(self, width, *args): + return self.__class__(self.data.rjust(width, *args)) def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars)) def split(self, sep=None, maxsplit=-1): return self.data.split(sep, maxsplit) diff --git a/Lib/string.py b/Lib/string.py index 5b5ddc3..0a77f46 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -237,37 +237,37 @@ def atol(s, base=10): # Left-justify a string -def ljust(s, width): - """ljust(s, width) -> string +def ljust(s, width, *args): + """ljust(s, width[, fillchar]) -> string Return a left-justified version of s, in a field of the specified width, padded with spaces as needed. The string is - never truncated. + never truncated. If specified the fillchar is used instead of spaces. """ - return s.ljust(width) + return s.ljust(width, *args) # Right-justify a string -def rjust(s, width): - """rjust(s, width) -> string +def rjust(s, width, *args): + """rjust(s, width[, fillchar]) -> string Return a right-justified version of s, in a field of the specified width, padded with spaces as needed. The string is - never truncated. + never truncated. If specified the fillchar is used instead of spaces. """ - return s.rjust(width) + return s.rjust(width, *args) # Center a string -def center(s, width): - """center(s, width) -> string +def center(s, width, *args): + """center(s, width[, fillchar]) -> string Return a center version of s, in a field of the specified width. padded with spaces as needed. The string is never - truncated. + truncated. If specified the fillchar is used instead of spaces. """ - return s.center(width) + return s.center(width, *args) # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' # Decadent feature: the argument may be a string or a number diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index af171d0..236c577 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -227,7 +227,7 @@ class CommonTest(unittest.TestCase): self.checkequal('abc ', 'abc', 'ljust', 6) self.checkequal('abc', 'abc', 'ljust', 3) self.checkequal('abc', 'abc', 'ljust', 2) - + self.checkequal('abc*******', 'abc', 'ljust', 10, '*') self.checkraises(TypeError, 'abc', 'ljust') def test_rjust(self): @@ -235,7 +235,7 @@ class CommonTest(unittest.TestCase): self.checkequal(' abc', 'abc', 'rjust', 6) self.checkequal('abc', 'abc', 'rjust', 3) self.checkequal('abc', 'abc', 'rjust', 2) - + self.checkequal('*******abc', 'abc', 'rjust', 10, '*') self.checkraises(TypeError, 'abc', 'rjust') def test_center(self): @@ -243,7 +243,7 @@ class CommonTest(unittest.TestCase): self.checkequal(' abc ', 'abc', 'center', 6) self.checkequal('abc', 'abc', 'center', 3) self.checkequal('abc', 'abc', 'center', 2) - + self.checkequal('***abc****', 'abc', 'center', 10, '*') self.checkraises(TypeError, 'abc', 'center') def test_swapcase(self): |