Mercurial > cpython-lru_cache-weakref
annotate Lib/string.py @ 32975:9b188158181f legacy-trunk
Typo fix; use 'in-place' instead of 'inplace'
| author | Andrew M. Kuchling <amk@amk.ca> |
|---|---|
| date | Fri, 06 Aug 2004 18:55:09 +0000 |
| parents | 504943c89193 |
| children | 11a4fbc88a80 |
| rev | line source |
|---|---|
|
30633
c1a672ee1530
tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents:
28575
diff
changeset
|
1 """A collection of string operations (most are no longer used). |
| 5 | 2 |
|
30633
c1a672ee1530
tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents:
28575
diff
changeset
|
3 Warning: most of the code you see here isn't normally used nowadays. |
|
c1a672ee1530
tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents:
28575
diff
changeset
|
4 Beginning with Python 1.6, many of these functions are implemented as |
|
c1a672ee1530
tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents:
28575
diff
changeset
|
5 methods on the standard string object. They used to be implemented by |
|
c1a672ee1530
tweak the docstring to not be so focused on 1.6.
Skip Montanaro <skip@pobox.com>
parents:
28575
diff
changeset
|
6 a built-in module called strop, but strop is now obsolete itself. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
7 |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
8 Public module variables: |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
9 |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
10 whitespace -- a string containing all characters considered whitespace |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
11 lowercase -- a string containing all characters considered lowercase letters |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
12 uppercase -- a string containing all characters considered uppercase letters |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
13 letters -- a string containing all characters considered letters |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
14 digits -- a string containing all characters considered decimal digits |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
15 hexdigits -- a string containing all characters considered hexadecimal digits |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
16 octdigits -- a string containing all characters considered octal digits |
| 14670 | 17 punctuation -- a string containing all characters considered punctuation |
| 18 printable -- a string containing all characters considered printable | |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
19 |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
20 """ |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
21 |
| 5 | 22 # Some strings for ctype-style character classification |
|
1331
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
23 whitespace = ' \t\n\r\v\f' |
| 5 | 24 lowercase = 'abcdefghijklmnopqrstuvwxyz' |
| 25 uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
| 26 letters = lowercase + uppercase | |
|
18776
22b2f4b436af
Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
Fred Drake <fdrake@acm.org>
parents:
16657
diff
changeset
|
27 ascii_lowercase = lowercase |
|
22b2f4b436af
Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
Fred Drake <fdrake@acm.org>
parents:
16657
diff
changeset
|
28 ascii_uppercase = uppercase |
|
22b2f4b436af
Added the constants ascii_letters, ascii_lowercase, and ascii_uppercase
Fred Drake <fdrake@acm.org>
parents:
16657
diff
changeset
|
29 ascii_letters = ascii_lowercase + ascii_uppercase |
| 5 | 30 digits = '0123456789' |
| 31 hexdigits = digits + 'abcdef' + 'ABCDEF' | |
| 32 octdigits = '01234567' | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15610
diff
changeset
|
33 punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
|
14665
6fcf5a70533c
Richard Mortier <rmm1002@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents:
13620
diff
changeset
|
34 printable = digits + letters + punctuation + whitespace |
| 5 | 35 |
| 36 # Case conversion helpers | |
|
25786
af20aeaaa982
Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents:
23645
diff
changeset
|
37 # Use str to convert Unicode literal in case of -U |
|
af20aeaaa982
Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents:
23645
diff
changeset
|
38 l = map(chr, xrange(256)) |
|
af20aeaaa982
Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents:
23645
diff
changeset
|
39 _idmap = str('').join(l) |
|
af20aeaaa982
Convert empty string literal to string. Speed up creation of idmap.
Martin v. Löwis <martin@v.loewis.de>
parents:
23645
diff
changeset
|
40 del l |
| 5 | 41 |
|
1788
56a986e49f56
* Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
42 # Backward compatible names for exceptions |
|
56a986e49f56
* Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
43 index_error = ValueError |
|
56a986e49f56
* Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
44 atoi_error = ValueError |
|
56a986e49f56
* Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
45 atof_error = ValueError |
|
56a986e49f56
* Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
46 atol_error = ValueError |
|
56a986e49f56
* Lib/string.py: find/rfind is now the main implementation and
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
47 |
| 5 | 48 # convert UPPER CASE letters to lower case |
| 49 def lower(s): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
50 """lower(s) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
51 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
52 Return a copy of the string s converted to lowercase. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
53 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
54 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
55 return s.lower() |
| 5 | 56 |
| 57 # Convert lower case letters to UPPER CASE | |
| 58 def upper(s): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
59 """upper(s) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
60 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
61 Return a copy of the string s converted to uppercase. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
62 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
63 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
64 return s.upper() |
| 5 | 65 |
| 66 # Swap lower case letters and UPPER CASE | |
| 67 def swapcase(s): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
68 """swapcase(s) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
69 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
70 Return a copy of the string s with upper case characters |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
71 converted to lowercase and vice versa. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
72 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
73 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
74 return s.swapcase() |
| 5 | 75 |
| 76 # Strip leading and trailing tabs and spaces | |
|
26107
ef3f6929fc0e
Make strip behave as documented. Will backport to 2.2.3.
Martin v. Löwis <martin@v.loewis.de>
parents:
25786
diff
changeset
|
77 def strip(s, chars=None): |
|
26213
a5d1e0814061
Update the docstring to match the code. Will backport.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26107
diff
changeset
|
78 """strip(s [,chars]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
79 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
80 Return a copy of the string s with leading and trailing |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
81 whitespace removed. |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
82 If chars is given and not None, remove characters in chars instead. |
|
26213
a5d1e0814061
Update the docstring to match the code. Will backport.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26107
diff
changeset
|
83 If chars is unicode, S will be converted to unicode before stripping. |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
84 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
85 """ |
|
26107
ef3f6929fc0e
Make strip behave as documented. Will backport to 2.2.3.
Martin v. Löwis <martin@v.loewis.de>
parents:
25786
diff
changeset
|
86 return s.strip(chars) |
| 5 | 87 |
|
3957
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
88 # Strip leading tabs and spaces |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
89 def lstrip(s, chars=None): |
|
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
90 """lstrip(s [,chars]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
91 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
92 Return a copy of the string s with leading whitespace removed. |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
93 If chars is given and not None, remove characters in chars instead. |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
94 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
95 """ |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
96 return s.lstrip(chars) |
|
3957
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
97 |
|
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
98 # Strip trailing tabs and spaces |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
99 def rstrip(s, chars=None): |
|
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
100 """rstrip(s [,chars]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
101 |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
102 Return a copy of the string s with trailing whitespace removed. |
|
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
103 If chars is given and not None, remove characters in chars instead. |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
104 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
105 """ |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
26213
diff
changeset
|
106 return s.rstrip(chars) |
|
3957
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
107 |
|
f01e7af1dd60
Add optional third parameter to split() and splitfields(), giving the
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
108 |
| 5 | 109 # Split a string into a list of space/tab-separated words |
|
11991
81702a7354ec
Marc-Andre Lemburg: the maxsplit argument for split() and replace()
Guido van Rossum <guido@python.org>
parents:
11741
diff
changeset
|
110 def split(s, sep=None, maxsplit=-1): |
|
11439
316be08c9ecd
split() docstring: Made signature and description for the first
Fred Drake <fdrake@acm.org>
parents:
11383
diff
changeset
|
111 """split(s [,sep [,maxsplit]]) -> list of strings |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
112 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
113 Return a list of the words in the string s, using sep as the |
|
21847
45731f1360ee
string.split() docstring described the interpretation of the maxsplit
Fred Drake <fdrake@acm.org>
parents:
18776
diff
changeset
|
114 delimiter string. If maxsplit is given, splits at no more than |
|
45731f1360ee
string.split() docstring described the interpretation of the maxsplit
Fred Drake <fdrake@acm.org>
parents:
18776
diff
changeset
|
115 maxsplit places (resulting in at most maxsplit+1 words). If sep |
|
45731f1360ee
string.split() docstring described the interpretation of the maxsplit
Fred Drake <fdrake@acm.org>
parents:
18776
diff
changeset
|
116 is not specified, any whitespace string is a separator. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
117 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
118 (split and splitfields are synonymous) |
| 5 | 119 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
120 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
121 return s.split(sep, maxsplit) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
122 splitfields = split |
|
196
48fc0b8f33d9
Added join() and joinfields() functions.
Guido van Rossum <guido@python.org>
parents:
102
diff
changeset
|
123 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
124 # Split a string into a list of space/tab-separated words |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
125 def rsplit(s, sep=None, maxsplit=-1): |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
126 """rsplit(s [,sep [,maxsplit]]) -> list of strings |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
127 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
128 Return a list of the words in the string s, using sep as the |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
129 delimiter string, starting at the end of the string and working |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
130 to the front. If maxsplit is given, at most maxsplit splits are |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
131 done. If sep is not specified or is None, any whitespace string |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
132 is a separator. |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
133 """ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
134 return s.rsplit(sep, maxsplit) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31021
diff
changeset
|
135 |
|
2778
17fdec2fb309
make split and splitfields, join and joinfields synonyms
Guido van Rossum <guido@python.org>
parents:
2541
diff
changeset
|
136 # Join fields with optional separator |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
137 def join(words, sep = ' '): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
138 """join(list [,sep]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
139 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
140 Return a string composed of the words in list, with |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13315
diff
changeset
|
141 intervening occurrences of sep. The default separator is a |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
142 single space. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
143 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
144 (joinfields and join are synonymous) |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
145 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
146 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
147 return sep.join(words) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
148 joinfields = join |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
149 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
150 # Find substring, raise exception if not found |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
151 def index(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
152 """index(s, sub [,start [,end]]) -> int |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
153 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
154 Like find but raises ValueError when the substring is not found. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
155 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
156 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
157 return s.index(*args) |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
158 |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
159 # Find last substring, raise exception if not found |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
160 def rindex(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
161 """rindex(s, sub [,start [,end]]) -> int |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
162 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
163 Like rfind but raises ValueError when the substring is not found. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
164 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
165 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
166 return s.rindex(*args) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
167 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
168 # Count non-overlapping occurrences of substring |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
169 def count(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
170 """count(s, sub[, start[,end]]) -> int |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
171 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
172 Return the number of occurrences of substring sub in string |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
173 s[start:end]. Optional arguments start and end are |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
174 interpreted as in slice notation. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
175 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
176 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
177 return s.count(*args) |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
178 |
|
1291
517d29ebc840
* ftplib.py: added abort() command (sends oob data).
Guido van Rossum <guido@python.org>
parents:
1260
diff
changeset
|
179 # Find substring, return -1 if not found |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
180 def find(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
181 """find(s, sub [,start [,end]]) -> in |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
182 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
183 Return the lowest index in s where substring sub is found, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
184 such that sub is contained within s[start,end]. Optional |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
185 arguments start and end are interpreted as in slice notation. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
186 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
187 Return -1 on failure. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
188 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
189 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
190 return s.find(*args) |
| 5 | 191 |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
192 # Find last substring, return -1 if not found |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
193 def rfind(s, *args): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
194 """rfind(s, sub [,start [,end]]) -> int |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
195 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
196 Return the highest index in s where substring sub is found, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
197 such that sub is contained within s[start,end]. Optional |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
198 arguments start and end are interpreted as in slice notation. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
199 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
200 Return -1 on failure. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
201 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
202 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
203 return s.rfind(*args) |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1357
diff
changeset
|
204 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
205 # for a bit of speed |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
206 _float = float |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
207 _int = int |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
208 _long = long |
|
6821
b19cd4313126
At Barry's suggestion, plug the security leak by using an empty
Guido van Rossum <guido@python.org>
parents:
6820
diff
changeset
|
209 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
210 # Convert string to float |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
211 def atof(s): |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
212 """atof(s) -> float |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
213 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
214 Return the floating point number represented by the string s. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
215 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
216 """ |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11991
diff
changeset
|
217 return _float(s) |
|
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11991
diff
changeset
|
218 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
219 |
| 5 | 220 # Convert string to integer |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11991
diff
changeset
|
221 def atoi(s , base=10): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
222 """atoi(s [,base]) -> int |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
223 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
224 Return the integer represented by the string s in the given |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
225 base, which defaults to 10. The string s must consist of one |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
226 or more digits, possibly preceded by a sign. If base is 0, it |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
227 is chosen from the leading characters of s, 0 for octal, 0x or |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
228 0X for hexadecimal. If base is 16, a preceding 0x or 0X is |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
229 accepted. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
230 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
231 """ |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11991
diff
changeset
|
232 return _int(s, base) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
233 |
| 5 | 234 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
235 # Convert string to long integer |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11991
diff
changeset
|
236 def atol(s, base=10): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
237 """atol(s [,base]) -> long |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
238 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
239 Return the long integer represented by the string s in the |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
240 given base, which defaults to 10. The string s must consist |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
241 of one or more digits, possibly preceded by a sign. If base |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
242 is 0, it is chosen from the leading characters of s, 0 for |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
243 octal, 0x or 0X for hexadecimal. If base is 16, a preceding |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
244 0x or 0X is accepted. A trailing L or l is not accepted, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
245 unless base is 0. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
246 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
247 """ |
|
12248
70154e2e9c89
Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents:
11991
diff
changeset
|
248 return _long(s, base) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
249 |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1331
diff
changeset
|
250 |
| 5 | 251 # Left-justify a string |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
252 def ljust(s, width, *args): |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
253 """ljust(s, width[, fillchar]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
254 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
255 Return a left-justified version of s, in a field of the |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
256 specified width, padded with spaces as needed. The string is |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
257 never truncated. If specified the fillchar is used instead of spaces. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
258 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
259 """ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
260 return s.ljust(width, *args) |
| 5 | 261 |
| 262 # Right-justify a string | |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
263 def rjust(s, width, *args): |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
264 """rjust(s, width[, fillchar]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
265 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
266 Return a right-justified version of s, in a field of the |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
267 specified width, padded with spaces as needed. The string is |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
268 never truncated. If specified the fillchar is used instead of spaces. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
269 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
270 """ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
271 return s.rjust(width, *args) |
| 5 | 272 |
| 273 # Center a string | |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
274 def center(s, width, *args): |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
275 """center(s, width[, fillchar]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
276 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
277 Return a center version of s, in a field of the specified |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
278 width. padded with spaces as needed. The string is never |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
279 truncated. If specified the fillchar is used instead of spaces. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
280 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
281 """ |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30633
diff
changeset
|
282 return s.center(width, *args) |
| 5 | 283 |
| 284 # Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03' | |
| 285 # Decadent feature: the argument may be a string or a number | |
| 286 # (Use of this is deprecated; it should be a string as with ljust c.s.) | |
| 287 def zfill(x, width): | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
288 """zfill(x, width) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
289 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
290 Pad a numeric string x with zeros on the left, to fill a field |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
291 of the specified width. The string x is never truncated. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
292 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
293 """ |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23045
diff
changeset
|
294 if not isinstance(x, basestring): |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
22660
diff
changeset
|
295 x = repr(x) |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
22660
diff
changeset
|
296 return x.zfill(width) |
|
724
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
297 |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
298 # Expand tabs in a string. |
|
476bb7e5b854
Add function to expand tabs.
Guido van Rossum <guido@python.org>
parents:
585
diff
changeset
|
299 # Doesn't take non-printing chars into account, but does understand \n. |
| 2958 | 300 def expandtabs(s, tabsize=8): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
301 """expandtabs(s [,tabsize]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
302 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
303 Return a copy of the string s with all tab characters replaced |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
304 by the appropriate number of spaces, depending on the current |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
305 column, and the tabsize (default 8). |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
306 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
307 """ |
|
13315
58183679c90a
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
Fred Drake <fdrake@acm.org>
parents:
12248
diff
changeset
|
308 return s.expandtabs(tabsize) |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
309 |
|
3639
70183592370e
add translate() -- which was in strop per release 1.3
Guido van Rossum <guido@python.org>
parents:
2958
diff
changeset
|
310 # Character translation through look-up table. |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3669
diff
changeset
|
311 def translate(s, table, deletions=""): |
|
15610
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
312 """translate(s,table [,deletions]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
313 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
314 Return a copy of the string s, where all characters occurring |
|
15610
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
315 in the optional argument deletions are removed, and the |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
316 remaining characters have been mapped through the given |
|
15610
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
317 translation table, which must be a string of length 256. The |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
318 deletions argument is not allowed for Unicode strings. |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
319 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
320 """ |
|
15610
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
321 if deletions: |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
322 return s.translate(table, deletions) |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
323 else: |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
324 # Add s[:0] so that if s is Unicode and table is an 8-bit string, |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
325 # table is converted to Unicode. This means that table *cannot* |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
326 # be a dictionary -- for that feature, use u.translate() directly. |
|
ed315b55f19e
Make string.translate(s, table) work for Unicode s. Two things are
Guido van Rossum <guido@python.org>
parents:
14670
diff
changeset
|
327 return s.translate(table + s[:0]) |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
328 |
|
3669
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
329 # Capitalize a string, e.g. "aBc dEf" -> "Abc def". |
|
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
330 def capitalize(s): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
331 """capitalize(s) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
332 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
333 Return a copy of the string s with only its first character |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
334 capitalized. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
335 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
336 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
337 return s.capitalize() |
|
3669
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
338 |
|
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
339 # Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def". |
|
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
340 # See also regsub.capwords(). |
|
4076
eda29bef880c
Add optional separator character to capwords(), for completeness.
Guido van Rossum <guido@python.org>
parents:
3957
diff
changeset
|
341 def capwords(s, sep=None): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
342 """capwords(s, [sep]) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
343 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
344 Split the argument into words using split, capitalize each |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
345 word using capitalize, and join the capitalized words using |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
346 join. Note that this replaces runs of whitespace characters by |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
347 a single space. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
348 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
349 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
350 return join(map(capitalize, s.split(sep)), sep or ' ') |
|
3669
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
351 |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3669
diff
changeset
|
352 # Construct a translation string |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3669
diff
changeset
|
353 _idmapL = None |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3669
diff
changeset
|
354 def maketrans(fromstr, tostr): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
355 """maketrans(frm, to) -> string |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
356 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
357 Return a translation table (a string of 256 bytes long) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
358 suitable for use in string.translate. The strings frm and to |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
359 must be of the same length. |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
360 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
361 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
362 if len(fromstr) != len(tostr): |
| 11741 | 363 raise ValueError, "maketrans arguments must have same length" |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
364 global _idmapL |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
365 if not _idmapL: |
| 11741 | 366 _idmapL = map(None, _idmap) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
367 L = _idmapL[:] |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
368 fromstr = map(ord, fromstr) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
369 for i in range(len(fromstr)): |
| 11741 | 370 L[fromstr[i]] = tostr[i] |
|
16532
7fc64df13db0
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15872
diff
changeset
|
371 return join(L, "") |
|
3669
1d134ecda094
Added capitalize() and capwords().
Guido van Rossum <guido@python.org>
parents:
3639
diff
changeset
|
372 |
|
5213
5b407e0cbc71
Added new functions replace() and replace1().
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
373 # Substring replacement (global) |
|
11991
81702a7354ec
Marc-Andre Lemburg: the maxsplit argument for split() and replace()
Guido van Rossum <guido@python.org>
parents:
11741
diff
changeset
|
374 def replace(s, old, new, maxsplit=-1): |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
375 """replace (str, old, new[, maxsplit]) -> string |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
376 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
377 Return a copy of string str with all occurrences of substring |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
378 old replaced by new. If the optional argument maxsplit is |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
379 given, only the first maxsplit occurrences are replaced. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
380 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
381 """ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
382 return s.replace(old, new, maxsplit) |
|
6964
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
383 |
|
33be7d5d498b
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6821
diff
changeset
|
384 |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
385 # Try importing optional built-in module "strop" -- if it exists, |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
386 # it redefines some string operations that are 100-1000 times faster. |
|
1331
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
387 # It also defines values for whitespace, lowercase and uppercase |
|
07900834cc97
* pdb.py: set 'privileged' property when evaluating expressions
Guido van Rossum <guido@python.org>
parents:
1291
diff
changeset
|
388 # that match <ctype.h>'s definitions. |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
389 |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
1052
diff
changeset
|
390 try: |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
391 from strop import maketrans, lowercase, uppercase, whitespace |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
11379
diff
changeset
|
392 letters = lowercase + uppercase |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1403
diff
changeset
|
393 except ImportError: |
| 11741 | 394 pass # Use the original versions |
