Changeset 391 for python/trunk/Doc/library/string.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/string.rst
r2 r391 7 7 8 8 .. index:: module: re 9 10 **Source code:** :source:`Lib/string.py` 11 12 -------------- 9 13 10 14 The :mod:`string` module contains a number of useful constants and … … 18 22 string functions based on regular expressions. 19 23 20 21 24 String constants 22 25 ---------------- … … 106 109 ----------------- 107 110 108 Starting in Python 2.6, the built-in str and unicode classes provide the ability 111 .. versionadded:: 2.6 112 113 The built-in str and unicode classes provide the ability 109 114 to do complex variable substitutions and value formatting via the 110 115 :meth:`str.format` method described in :pep:`3101`. The :class:`Formatter` … … 117 122 The :class:`Formatter` class has the following public methods: 118 123 119 .. method:: format(format_string, *args, * kwargs)120 121 :meth:`format` is the primary API method. It takes a format template122 string, and an arbitrary set of positional and keyword argument.124 .. method:: format(format_string, *args, **kwargs) 125 126 :meth:`format` is the primary API method. It takes a format string and 127 an arbitrary set of positional and keyword arguments. 123 128 :meth:`format` is just a wrapper that calls :meth:`vformat`. 124 129 … … 128 133 separate function for cases where you want to pass in a predefined 129 134 dictionary of arguments, rather than unpacking and repacking the 130 dictionary as individual arguments using the ``*args`` and ``**kw ds``131 syntax. :meth:`vformat` does the work of breaking up the format template132 stringinto character data and replacement fields. It calls the various135 dictionary as individual arguments using the ``*args`` and ``**kwargs`` 136 syntax. :meth:`vformat` does the work of breaking up the format string 137 into character data and replacement fields. It calls the various 133 138 methods described below. 134 139 … … 140 145 Loop over the format_string and return an iterable of tuples 141 146 (*literal_text*, *field_name*, *format_spec*, *conversion*). This is used 142 by :meth:`vformat` to break the string in 147 by :meth:`vformat` to break the string into either literal text, or 143 148 replacement fields. 144 149 … … 189 194 named arguments), and a reference to the *args* and *kwargs* that was 190 195 passed to vformat. The set of unused args can be calculated from these 191 parameters. :meth:`check_unused_args` is assumed to throwan exception if196 parameters. :meth:`check_unused_args` is assumed to raise an exception if 192 197 the check fails. 193 198 … … 200 205 201 206 Converts the value (returned by :meth:`get_field`) given a conversion type 202 (as in the tuple returned by the :meth:`parse` method.) The default 203 version understands 'r' (repr) and 's' (str) conversion types. 207 (as in the tuple returned by the :meth:`parse` method). The default 208 version understands 's' (str), 'r' (repr) and 'a' (ascii) conversion 209 types. 204 210 205 211 … … 211 217 The :meth:`str.format` method and the :class:`Formatter` class share the same 212 218 syntax for format strings (although in the case of :class:`Formatter`, 213 subclasses can define their own format string syntax .)219 subclasses can define their own format string syntax). 214 220 215 221 Format strings contain "replacement fields" surrounded by curly braces ``{}``. … … 221 227 222 228 .. productionlist:: sf 223 replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}" 224 field_name: (`identifier` | `integer`) ("." `attribute_name` | "[" `element_index` "]")* 229 replacement_field: "{" [`field_name`] ["!" `conversion`] [":" `format_spec`] "}" 230 field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")* 231 arg_name: [`identifier` | `integer`] 225 232 attribute_name: `identifier` 226 233 element_index: `integer` | `index_string` … … 229 236 format_spec: <described in the next section> 230 237 231 In less formal terms, the replacement field starts with a *field_name*, which 232 can either be a number (for a positional argument), or an identifier (for 233 keyword arguments). Following this is an optional *conversion* field, which is 238 In less formal terms, the replacement field can start with a *field_name* that specifies 239 the object whose value is to be formatted and inserted 240 into the output instead of the replacement field. 241 The *field_name* is optionally followed by a *conversion* field, which is 234 242 preceded by an exclamation point ``'!'``, and a *format_spec*, which is preceded 235 by a colon ``':'``. 236 237 The *field_name* itself begins with either a number or a keyword. If it's a 238 number, it refers to a positional argument, and if it's a keyword it refers to a 239 named keyword argument. This can be followed by any number of index or 243 by a colon ``':'``. These specify a non-default format for the replacement value. 244 245 See also the :ref:`formatspec` section. 246 247 The *field_name* itself begins with an *arg_name* that is either a number or a 248 keyword. If it's a number, it refers to a positional argument, and if it's a keyword, 249 it refers to a named keyword argument. If the numerical arg_names in a format string 250 are 0, 1, 2, ... in sequence, they can all be omitted (not just some) 251 and the numbers 0, 1, 2, ... will be automatically inserted in that order. 252 Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary 253 dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string. 254 The *arg_name* can be followed by any number of index or 240 255 attribute expressions. An expression of the form ``'.name'`` selects the named 241 256 attribute using :func:`getattr`, while an expression of the form ``'[index]'`` 242 257 does an index lookup using :func:`__getitem__`. 243 258 259 .. versionchanged:: 2.7 260 The positional argument specifiers can be omitted, so ``'{} {}'`` is 261 equivalent to ``'{0} {1}'``. 262 244 263 Some simple format string examples:: 245 264 246 265 "First, thou shalt count to {0}" # References first positional argument 266 "Bring me a {}" # Implicitly references the first positional argument 267 "From {} to {}" # Same as "From {0} to {1}" 247 268 "My quest is {name}" # References keyword argument 'name' 248 269 "Weight in tons {0.weight}" # 'weight' attribute of first positional arg … … 278 299 This allows the formatting of a value to be dynamically specified. 279 300 280 For example, suppose you wanted to have a replacement field whose field width is 281 determined by another variable:: 282 283 "A man with two {0:{1}}".format("noses", 10) 284 285 This would first evaluate the inner replacement field, making the format string 286 effectively:: 287 288 "A man with two {0:10}" 289 290 Then the outer replacement field would be evaluated, producing:: 291 292 "noses " 293 294 Which is substituted into the string, yielding:: 295 296 "A man with two noses " 297 298 (The extra space is because we specified a field width of 10, and because left 299 alignment is the default for strings.) 301 See the :ref:`formatexamples` section for some examples. 300 302 301 303 … … 307 309 "Format specifications" are used within replacement fields contained within a 308 310 format string to define how individual values are presented (see 309 :ref:`formatstrings` .)They can also be passed directly to the built-in311 :ref:`formatstrings`). They can also be passed directly to the built-in 310 312 :func:`format` function. Each formattable type may define how the format 311 313 specification is to be interpreted. … … 321 323 322 324 .. productionlist:: sf 323 format_spec: [[`fill`]`align`][`sign`][#][0][`width`][ .`precision`][`type`]324 fill: <a character other than '}'>325 format_spec: [[`fill`]`align`][`sign`][#][0][`width`][,][.`precision`][`type`] 326 fill: <any character> 325 327 align: "<" | ">" | "=" | "^" 326 328 sign: "+" | "-" | " " … … 329 331 type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" 330 332 331 The *fill* character can be any character other than '}' (which signifies the 332 end of the field). The presence of a fill character is signaled by the *next* 333 character, which must be one of the alignment options. If the second character 334 of *format_spec* is not a valid alignment option, then it is assumed that both 335 the fill character and the alignment option are absent.333 If a valid *align* value is specified, it can be preceeded by a *fill* 334 character that can be any character and defaults to a space if omitted. 335 Note that it is not possible to use ``{`` and ``}`` as *fill* char while 336 using the :meth:`str.format` method; this limitation however doesn't 337 affect the :func:`format` function. 336 338 337 339 The meaning of the various alignment options is as follows: … … 341 343 +=========+==========================================================+ 342 344 | ``'<'`` | Forces the field to be left-aligned within the available | 343 | | space ( This is the default.)|345 | | space (this is the default for most objects). | 344 346 +---------+----------------------------------------------------------+ 345 347 | ``'>'`` | Forces the field to be right-aligned within the | 346 | | available space .|348 | | available space (this is the default for numbers). | 347 349 +---------+----------------------------------------------------------+ 348 350 | ``'='`` | Forces the padding to be placed after the sign (if any) | … … 379 381 by ``'0b'``, ``'0o'``, or ``'0x'``, respectively. 380 382 383 The ``','`` option signals the use of a comma for a thousands separator. 384 For a locale aware separator, use the ``'n'`` integer presentation type 385 instead. 386 387 .. versionchanged:: 2.7 388 Added the ``','`` option (see also :pep:`378`). 389 381 390 *width* is a decimal integer defining the minimum field width. If not 382 391 specified, then the field width will be determined by the content. 383 392 384 If the *width* field is preceded by a zero (``'0'``) character, thisenables385 zero-padding. This is equivalent to an *alignment* type of ``'='`` anda *fill*386 character of ``'0'`` .393 Preceding the *width* field by a zero (``'0'``) character enables 394 sign-aware zero-padding for numeric types. This is equivalent to a *fill* 395 character of ``'0'`` with an *alignment* type of ``'='``. 387 396 388 397 The *precision* is a decimal number indicating how many digits should be … … 445 454 | ``'e'`` | Exponent notation. Prints the number in scientific | 446 455 | | notation using the letter 'e' to indicate the exponent. | 456 | | The default precision is ``6``. | 447 457 +---------+----------------------------------------------------------+ 448 458 | ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an | … … 450 460 +---------+----------------------------------------------------------+ 451 461 | ``'f'`` | Fixed point. Displays the number as a fixed-point | 452 | | number. 462 | | number. The default precision is ``6``. | 453 463 +---------+----------------------------------------------------------+ 454 464 | ``'F'`` | Fixed point. Same as ``'f'``. | … … 470 480 | | removed if there are no remaining digits following it. | 471 481 | | | 472 | | Pos tive and negative infinity, positive and negative|482 | | Positive and negative infinity, positive and negative | 473 483 | | zero, and nans, are formatted as ``inf``, ``-inf``, | 474 484 | | ``0``, ``-0`` and ``nan`` respectively, regardless of | … … 476 486 | | | 477 487 | | A precision of ``0`` is treated as equivalent to a | 478 | | precision of ``1``. 488 | | precision of ``1``. The default precision is ``6``. | 479 489 +---------+----------------------------------------------------------+ 480 490 | ``'G'`` | General format. Same as ``'g'`` except switches to | … … 493 503 494 504 505 506 .. _formatexamples: 507 508 Format examples 509 ^^^^^^^^^^^^^^^ 510 511 This section contains examples of the new format syntax and comparison with 512 the old ``%``-formatting. 513 514 In most of the cases the syntax is similar to the old ``%``-formatting, with the 515 addition of the ``{}`` and with ``:`` used instead of ``%``. 516 For example, ``'%03.2f'`` can be translated to ``'{:03.2f}'``. 517 518 The new format syntax also supports new and different options, shown in the 519 follow examples. 520 521 Accessing arguments by position:: 522 523 >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 524 'a, b, c' 525 >>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only 526 'a, b, c' 527 >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 528 'c, b, a' 529 >>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence 530 'c, b, a' 531 >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated 532 'abracadabra' 533 534 Accessing arguments by name:: 535 536 >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') 537 'Coordinates: 37.24N, -115.81W' 538 >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'} 539 >>> 'Coordinates: {latitude}, {longitude}'.format(**coord) 540 'Coordinates: 37.24N, -115.81W' 541 542 Accessing arguments' attributes:: 543 544 >>> c = 3-5j 545 >>> ('The complex number {0} is formed from the real part {0.real} ' 546 ... 'and the imaginary part {0.imag}.').format(c) 547 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' 548 >>> class Point(object): 549 ... def __init__(self, x, y): 550 ... self.x, self.y = x, y 551 ... def __str__(self): 552 ... return 'Point({self.x}, {self.y})'.format(self=self) 553 ... 554 >>> str(Point(4, 2)) 555 'Point(4, 2)' 556 557 558 Accessing arguments' items:: 559 560 >>> coord = (3, 5) 561 >>> 'X: {0[0]}; Y: {0[1]}'.format(coord) 562 'X: 3; Y: 5' 563 564 Replacing ``%s`` and ``%r``:: 565 566 >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') 567 "repr() shows quotes: 'test1'; str() doesn't: test2" 568 569 Aligning the text and specifying a width:: 570 571 >>> '{:<30}'.format('left aligned') 572 'left aligned ' 573 >>> '{:>30}'.format('right aligned') 574 ' right aligned' 575 >>> '{:^30}'.format('centered') 576 ' centered ' 577 >>> '{:*^30}'.format('centered') # use '*' as a fill char 578 '***********centered***********' 579 580 Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign:: 581 582 >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always 583 '+3.140000; -3.140000' 584 >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers 585 ' 3.140000; -3.140000' 586 >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' 587 '3.140000; -3.140000' 588 589 Replacing ``%x`` and ``%o`` and converting the value to different bases:: 590 591 >>> # format also supports binary numbers 592 >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 593 'int: 42; hex: 2a; oct: 52; bin: 101010' 594 >>> # with 0x, 0o, or 0b as prefix: 595 >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 596 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' 597 598 Using the comma as a thousands separator:: 599 600 >>> '{:,}'.format(1234567890) 601 '1,234,567,890' 602 603 Expressing a percentage:: 604 605 >>> points = 19.5 606 >>> total = 22 607 >>> 'Correct answers: {:.2%}'.format(points/total) 608 'Correct answers: 88.64%' 609 610 Using type-specific formatting:: 611 612 >>> import datetime 613 >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) 614 >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) 615 '2010-07-04 12:15:58' 616 617 Nesting arguments and more complex examples:: 618 619 >>> for align, text in zip('<^>', ['left', 'center', 'right']): 620 ... '{0:{fill}{align}16}'.format(text, fill=align, align=align) 621 ... 622 'left<<<<<<<<<<<<' 623 '^^^^^center^^^^^' 624 '>>>>>>>>>>>right' 625 >>> 626 >>> octets = [192, 168, 0, 1] 627 >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets) 628 'C0A80001' 629 >>> int(_, 16) 630 3232235521 631 >>> 632 >>> width = 5 633 >>> for num in range(5,12): 634 ... for base in 'dXob': 635 ... print '{0:{width}{base}}'.format(num, base=base, width=width), 636 ... print 637 ... 638 5 5 5 101 639 6 6 6 110 640 7 7 7 111 641 8 8 10 1000 642 9 9 11 1001 643 10 A 12 1010 644 11 B 13 1011 645 646 647 495 648 Template strings 496 649 ---------------- 650 651 .. versionadded:: 2.4 497 652 498 653 Templates provide simpler string substitutions as described in :pep:`292`. … … 513 668 Any other appearance of ``$`` in the string will result in a :exc:`ValueError` 514 669 being raised. 515 516 .. versionadded:: 2.4517 670 518 671 The :mod:`string` module provides a :class:`Template` class that implements … … 549 702 placeholders that are not valid Python identifiers. 550 703 551 :class:`Template` instances also provide one public data attribute: 552 553 554 .. attribute:: string.template 555 556 This is the object passed to the constructor's *template* argument. In general, 557 you shouldn't change it, but read-only access is not enforced. 558 559 Here is an example of how to use a Template: 704 :class:`Template` instances also provide one public data attribute: 705 706 .. attribute:: template 707 708 This is the object passed to the constructor's *template* argument. In 709 general, you shouldn't change it, but read-only access is not enforced. 710 711 Here is an example of how to use a Template:: 560 712 561 713 >>> from string import Template … … 566 718 >>> Template('Give $who $100').substitute(d) 567 719 Traceback (most recent call last): 568 [...]569 ValueError: Invalid placeholder in string: line 1, col 1 0720 ... 721 ValueError: Invalid placeholder in string: line 1, col 11 570 722 >>> Template('$who likes $what').substitute(d) 571 723 Traceback (most recent call last): 572 [...]724 ... 573 725 KeyError: 'what' 574 726 >>> Template('$who likes $what').safe_substitute(d) … … 580 732 581 733 * *delimiter* -- This is the literal string describing a placeholder introducing 582 delimiter. The default value ``$``. Note that this should *not* be a regular583 expression, as the implementation will call :meth:`re.escape` on this string as584 needed.734 delimiter. The default value is ``$``. Note that this should *not* be a 735 regular expression, as the implementation will call :meth:`re.escape` on this 736 string as needed. 585 737 586 738 * *idpattern* -- This is the regular expression describing the pattern for … … 644 796 Unicode objects; see section :ref:`string-methods` for more information on 645 797 those. You should consider these functions as deprecated, although they will 646 not be removed until Python 3. 0.The functions defined in this module are:798 not be removed until Python 3. The functions defined in this module are: 647 799 648 800 … … 756 908 Return a list of the words of the string *s*. If the optional second argument 757 909 *sep* is absent or ``None``, the words are separated by arbitrary strings of 758 whitespace characters (space, tab, 910 whitespace characters (space, tab, newline, return, formfeed). If the second 759 911 argument *sep* is present and not ``None``, it specifies a string to be used as 760 912 the word separator. The returned list will then have one more item than the 761 number of non-overlapping occurrences of the separator in the string. The 762 optional third argument *maxsplit* defaults to 0. If it is nonzero, at most 763 *maxsplit* number of splits occur, and the remainder of the string is returned 764 as the final element of the list (thus, the list will have at most 765 ``maxsplit+1`` elements). 913 number of non-overlapping occurrences of the separator in the string. 914 If *maxsplit* is given, at most *maxsplit* number of splits occur, and the 915 remainder of the string is returned as the final element of the list (thus, 916 the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not 917 specified or ``-1``, then there is no limit on the number of splits (all 918 possible splits are made). 766 919 767 920 The behavior of split on an empty string depends on the value of *sep*. If *sep* … … 776 929 intents and purposes, the resulting list of words is the same as returned by 777 930 :func:`split`, except when the optional third argument *maxsplit* is explicitly 778 specified and nonzero. When *maxsplit* is nonzero, at most *maxsplit* number of931 specified and nonzero. If *maxsplit* is given, at most *maxsplit* number of 779 932 splits -- the *rightmost* ones -- occur, and the remainder of the string is 780 933 returned as the first element of the list (thus, the list will have at most … … 874 1027 .. function:: zfill(s, width) 875 1028 876 Pad a numeric string on the left with zero digits until the given width is 877 reached. Strings starting with a sign are handled correctly. 878 879 880 .. function:: replace(str, old, new[, maxreplace]) 881 882 Return a copy of string *str* with all occurrences of substring *old* replaced 1029 Pad a numeric string *s* on the left with zero digits until the 1030 given *width* is reached. Strings starting with a sign are handled 1031 correctly. 1032 1033 1034 .. function:: replace(s, old, new[, maxreplace]) 1035 1036 Return a copy of string *s* with all occurrences of substring *old* replaced 883 1037 by *new*. If the optional argument *maxreplace* is given, the first 884 1038 *maxreplace* occurrences are replaced.
Note:
See TracChangeset
for help on using the changeset viewer.