Sideway
output.to from Sideway
Draft for Information Only

Content

printf-style String Formatting
 Conversion Specifier
 Source and Reference

printf-style String Formatting



Strings also support two styles of string formatting, one providing a large degree of flexibility and customization (see 𝑠𝑡𝑟.format(), Format String Syntax and Custom String Formatting) and the other based on C printf style formatting that handles a narrower range of types and is slightly harder to use correctly, but is often faster for the cases it can handle (printf-style String Formatting).

The Text Processing Services section of the standard library covers a number of other modules that provide various text related utilities (including regular expression support in the re module).


Note

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the 𝑠𝑡𝑟.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.

String objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator. Given format % values (where format is a string), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to using the sprintf() in the C language.

If format requires a single argument, values may be a single non-tuple object. 5 Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary).

Conversion Specifier

A conversion specifier contains two or more characters and has the following components, which must occur in this order: %to mark the start of the specifier. (Mapping key) (optional), consisting of a parenthesised sequence of characters (for example, (somename)). Conversion flags (optional), which affect the result of some conversion types. The conversion flag characters are: FlagMeaning #The value conversion will use the “alternate form” (where defined below). 0The conversion will be zero padded for numeric values. -The converted value is left adjusted (overrides the '0' conversion if both are given).  (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. +A sign character ('+' or '-') will precede the conversion (overrides a “space” flag). A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python – so e.g. %ld is identical to %d. Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision. Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual precision is read from the next element of the tuple in values, and the value to convert comes after the precision. Length modifier (optional) A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python – so e.g. %ld is identical to %d.. Conversion typeThe conversion types are: TypeDescription dSigned integer decimal. iSigned integer decimal. oSigned octal value. uObsolete type – it is identical to 'd'. xSigned hexadecimal (lowercase). XSigned hexadecimal (uppercase). eFloating point exponential format (lowercase). EFloating point exponential format (uppercase). fFloating point decimal format. FFloating point decimal format. gFloating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. GFloating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. cSingle character (accepts integer or single character string). rString (converts any Python object using repr()). sString (converts any Python object using 𝑠𝑡𝑟()). aString (converts any Python object using ascii()). %No argument is converted, results in a '%' character in the result. Notes: The alternate form causes a leading octal specifier ('0o') to be inserted before the first digit. The alternate form causes a leading '0x' or '0X' (depending on whether the 'x' or 'X' format was used) to be inserted before the first digit. The alternate form causes the result to always contain a decimal point, even if no digits follow it. The precision determines the number of digits after the decimal point and defaults to 6. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be. The precision determines the number of significant digits before and after the decimal point and defaults to 6. If precision is N, the output is truncated to N characters. See PEP 237. Since Python strings have an explicit length, %s conversions do not assume that '\0' is the end of the string. Changed in version 3.1: %f conversions for numbers whose absolute value is over 1e50 are no longer replaced by %g conversions.

    
When the right argument is a dictionary (or other mapping type), then the formats in the string must include a parenthesised mapping key into that dictionary inserted immediately after the '%' character. The mapping key selects the value to be formatted from the mapping. For example:
>>>

>>> print('%(language)s has %(number)03d quote types.' %
...       {'language': "Python", "number": 2})
Python has 002 quote types.

In this case no * specifiers may occur in a format (since they require a sequential parameter list).

Source and Reference


©sideway

ID: 210100018 Last Updated: 1/18/2021 Revision: 0


Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019