mirror of
https://github.com/python/cpython.git
synced 2025-10-30 13:11:29 +00:00
Added join() and joinfields() functions.
Fixed center(). Rewrote ljust() and rjust().
This commit is contained in:
parent
2d844d1ddc
commit
fac38b7c40
2 changed files with 54 additions and 18 deletions
|
|
@ -79,6 +79,20 @@ def splitfields(s, sep):
|
||||||
res.append(s[i:])
|
res.append(s[i:])
|
||||||
return res
|
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
|
# Find substring
|
||||||
index_error = 'substring not found in string.index'
|
index_error = 'substring not found in string.index'
|
||||||
def index(s, sub):
|
def index(s, sub):
|
||||||
|
|
@ -99,21 +113,25 @@ def atoi(str):
|
||||||
|
|
||||||
# Left-justify a string
|
# Left-justify a string
|
||||||
def ljust(s, width):
|
def ljust(s, width):
|
||||||
n = len(s)
|
n = width - len(s)
|
||||||
if n >= width: return s
|
if n <= 0: return s
|
||||||
return s + ' '*(width-n)
|
return s + ' '*n
|
||||||
|
|
||||||
# Right-justify a string
|
# Right-justify a string
|
||||||
def rjust(s, width):
|
def rjust(s, width):
|
||||||
n = len(s)
|
n = width - len(s)
|
||||||
if n >= width: return s
|
if n <= 0: return s
|
||||||
return ' '*(width-n) + s
|
return ' '*n + s
|
||||||
|
|
||||||
# Center a string
|
# Center a string
|
||||||
def center(s, width):
|
def center(s, width):
|
||||||
n = len(s)
|
n = width - len(s)
|
||||||
if n >= width: return s
|
if n <= 0: return s
|
||||||
return ' '*((width-n)/2) + s + ' '*(width -(width-n)/2)
|
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'
|
# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
|
||||||
# Decadent feature: the argument may be a string or a number
|
# Decadent feature: the argument may be a string or a number
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,20 @@ def splitfields(s, sep):
|
||||||
res.append(s[i:])
|
res.append(s[i:])
|
||||||
return res
|
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
|
# Find substring
|
||||||
index_error = 'substring not found in string.index'
|
index_error = 'substring not found in string.index'
|
||||||
def index(s, sub):
|
def index(s, sub):
|
||||||
|
|
@ -99,21 +113,25 @@ def atoi(str):
|
||||||
|
|
||||||
# Left-justify a string
|
# Left-justify a string
|
||||||
def ljust(s, width):
|
def ljust(s, width):
|
||||||
n = len(s)
|
n = width - len(s)
|
||||||
if n >= width: return s
|
if n <= 0: return s
|
||||||
return s + ' '*(width-n)
|
return s + ' '*n
|
||||||
|
|
||||||
# Right-justify a string
|
# Right-justify a string
|
||||||
def rjust(s, width):
|
def rjust(s, width):
|
||||||
n = len(s)
|
n = width - len(s)
|
||||||
if n >= width: return s
|
if n <= 0: return s
|
||||||
return ' '*(width-n) + s
|
return ' '*n + s
|
||||||
|
|
||||||
# Center a string
|
# Center a string
|
||||||
def center(s, width):
|
def center(s, width):
|
||||||
n = len(s)
|
n = width - len(s)
|
||||||
if n >= width: return s
|
if n <= 0: return s
|
||||||
return ' '*((width-n)/2) + s + ' '*(width -(width-n)/2)
|
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'
|
# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
|
||||||
# Decadent feature: the argument may be a string or a number
|
# Decadent feature: the argument may be a string or a number
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue