diff options
author | Guido van Rossum <guido@python.org> | 1991-04-07 13:42:19 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1991-04-07 13:42:19 +0000 |
commit | 43271577e8ebe4b1336ae06e793d6be77f5c3b2c (patch) | |
tree | 073efa7a3d40f48c707bd00f75a9a346cc8e0be3 | |
parent | b06455d66b2ffd4e8b021d21cfcff0ff923deb29 (diff) | |
download | cpython-43271577e8ebe4b1336ae06e793d6be77f5c3b2c.tar.gz |
Added join() and joinfields() functions.
Fixed center().
Rewrote ljust() and rjust().
-rw-r--r-- | Lib/string.py | 36 | ||||
-rw-r--r-- | Lib/stringold.py | 36 |
2 files changed, 54 insertions, 18 deletions
diff --git a/Lib/string.py b/Lib/string.py index 3790357f1b..c245a493af 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -79,6 +79,20 @@ def splitfields(s, sep): res.append(s[i:]) return res +# Join words with spaces between them +def join(words): + res = '' + for w in words: + res = res + (' ' + w) + return res[1:] + +# Join fields with separator +def joinfields(words, sep): + res = '' + for w in words: + res = res + (sep + w) + return res[len(sep):] + # Find substring index_error = 'substring not found in string.index' def index(s, sub): @@ -99,21 +113,25 @@ def atoi(str): # Left-justify a string def ljust(s, width): - n = len(s) - if n >= width: return s - return s + ' '*(width-n) + n = width - len(s) + if n <= 0: return s + return s + ' '*n # Right-justify a string def rjust(s, width): - n = len(s) - if n >= width: return s - return ' '*(width-n) + s + n = width - len(s) + if n <= 0: return s + return ' '*n + s # Center a string def center(s, width): - n = len(s) - if n >= width: return s - return ' '*((width-n)/2) + s + ' '*(width -(width-n)/2) + n = width - len(s) + if n <= 0: return s + half = n/2 + if n%2 and width%2: + # This ensures that center(center(s, i), j) = center(s, j) + half = half+1 + return ' '*half + s + ' '*(n-half) # 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/stringold.py b/Lib/stringold.py index 3790357f1b..c245a493af 100644 --- a/Lib/stringold.py +++ b/Lib/stringold.py @@ -79,6 +79,20 @@ def splitfields(s, sep): res.append(s[i:]) return res +# Join words with spaces between them +def join(words): + res = '' + for w in words: + res = res + (' ' + w) + return res[1:] + +# Join fields with separator +def joinfields(words, sep): + res = '' + for w in words: + res = res + (sep + w) + return res[len(sep):] + # Find substring index_error = 'substring not found in string.index' def index(s, sub): @@ -99,21 +113,25 @@ def atoi(str): # Left-justify a string def ljust(s, width): - n = len(s) - if n >= width: return s - return s + ' '*(width-n) + n = width - len(s) + if n <= 0: return s + return s + ' '*n # Right-justify a string def rjust(s, width): - n = len(s) - if n >= width: return s - return ' '*(width-n) + s + n = width - len(s) + if n <= 0: return s + return ' '*n + s # Center a string def center(s, width): - n = len(s) - if n >= width: return s - return ' '*((width-n)/2) + s + ' '*(width -(width-n)/2) + n = width - len(s) + if n <= 0: return s + half = n/2 + if n%2 and width%2: + # This ensures that center(center(s, i), j) = center(s, j) + half = half+1 + return ' '*half + s + ' '*(n-half) # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' # Decadent feature: the argument may be a string or a number |