% (String Formatting Operator) — Python Reference (The Right Way) 0.1 documentation (2024)

Description

Formats the string according to the specified format.

Syntax

%[key][flags][width][.precision][length type]conversion type % values

%
Required. The ‘%’ character, which marks the start of the specifier.
key
Optional. Mapping key, consisting of a parenthesised sequence of characters (for example, (somename)).
flags
Optional. Conversion flags, which affect the result of some conversion types.
width
Optional. Minimum field width. 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. Precision, given as a ‘.’ (dot) followed by the precision. If specified as ‘*’ (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision.
length type
Optional. Length modifier.
conversion type
Optional. Conversion type.
values
Required. A number, string or a container with values to substitute for the conversion type.

Conversion Flags

‘#’
The value conversion will use the “alternate form” (where defined below).
‘0’
The 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.

Conversion Types

‘d’
Signed integer decimal.
‘i’
Signed integer decimal.
‘o’
Signed octal value. The alternate form causes a leading zero (‘0’) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
‘u’
Obsolete type – it is identical to ‘d’. See PEP 237.
‘x’ or ‘X’
Signed hexadecimal. The alternate form causes a leading ‘0x’ or ‘0X’ (depending on whether the ‘x’ or ‘X’ format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
‘e’ or ‘E’

Floating point exponential format. 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.

‘f’ or ‘F’

Floating point decimal format. 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.

‘g’ or ‘G’

Floating point format. Uses exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. 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.

‘c’
Single character (accepts integer or single character string).
‘r’

String (converts any Python object using repr()).

The precision determines the maximal number of characters used.

‘s’

String (converts any Python object using str()).

If the object or format provided is a unicode string, the resulting string will also be unicode.

The precision determines the maximal number of characters used.

‘%’
No argument is converted, results in a ‘%’ character in the result.

Remarks

If format specifier is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.

If format specifier requires a single argument, values may be a single non-tuple object. 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).

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).

Example 1

>>> '%d' % 100 # This Example shows simple substitution.'100'>>> '%d' % 0b1111'15'>>> "%s" % 'foo''foo'>>> "%s %s" % ('foo', 'bar')'foo bar'>>> dct = {'foo': 10, 'bar': 20}>>> "%(foo)s" % dct'10'

Example 2

>>> "%x" % 17 # This Example shows usage of # flag (alternate form).'11'>>> "%#x" % 17'0x11'>>> "%#X" % 17'0X11'

Example 3

>>> "%d" % 1 # This Example shows usage of zero padding and width flags.'1'>>> "%03d" % 1'001'

Example 4

>>> "%d" % 1 # This Example shows usage of left adjust flag.'1'>>> "%-5d" % 1'1 '>>> "%0-5d" % 1'1 '

Example 5

>>> '%d' % 1 # This Example shows usage of space flag.'1'>>> '%d' % -1'-1'>>> '% d' % 1' 1'>>> '% d' % -1'-1'

Example 6

>>> '%d' % 1 # This Example shows usage of sign flag.'1'>>> '%+d' % 1'+1'>>> '%+d' % -1'-1'>>> '% +d' % -1'-1'>>> '% +d' % 1'+1'

Example 7

>>> '%f' % 3.14 # This Example shows usage of precision modifier.'3.140000'>>> '%.1f' % 3.14'3.1'>>> '%.2f' % 3.14'3.14'>>> '%.3f' % 3.14'3.140'

Example 8

>>> '%i' % 0b1111 # This Example shows how to covert to signed integer decimals.'15'>>> '%d' % 0b1111'15'>>> '%d' % 10'10'>>> '%d' % 3.14'3'>>> '%i' % 3.14'3'>>> '%i' % -10'-10'

Example 9

>>> '%x' % 27 # Hex and octal conversions.'1b'>>> '%X' % 27'1B'>>> '%o' % 27'33'

Example 10

>>> '%f' % 10 # Float conversions.'10.000000'>>> '%F' % 10'10.000000'>>> '%g' % 1234567890'1.23457e+09'>>> '%G' % 1234567890'1.23457E+09'>>> '%e' % 1234567890'1.234568e+09'>>> '%E' % 1234567890'1.234568E+09'

Example 11

>>> 'ABC %c' % 10 # Character and string conversions.'ABC \n'>>> 'ABC %c' % 67'ABC C'>>> 'ABC %c' % 68'ABC D'>>> 'ABC %c' % 'D''ABC D'>>> 'ABC %s' % 68'ABC 68'>>> 'ABC %r' % 68'ABC 68'

Read the Docs v: latest

Versions
latest
Downloads
pdf
htmlzip
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.

% (String Formatting Operator) — Python Reference (The Right Way) 0.1 documentation (2024)

References

Top Articles
Everything We Know About John Wick: Chapter 4
Shoreone Insurance A.m. Best Rating
Funny Roblox Id Codes 2023
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Joi Databas
DPhil Research - List of thesis titles
Shs Games 1V1 Lol
Evil Dead Rise Showtimes Near Massena Movieplex
Steamy Afternoon With Handsome Fernando
Which aspects are important in sales |#1 Prospection
Detroit Lions 50 50
18443168434
Newgate Honda
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Grace Caroline Deepfake
978-0137606801
Nwi Arrests Lake County
Justified Official Series Trailer
London Ups Store
Committees Of Correspondence | Encyclopedia.com
Pizza Hut In Dinuba
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Free Online Games on CrazyGames | Play Now!
Sizewise Stat Login
VERHUURD: Barentszstraat 12 in 'S-Gravenhage 2518 XG: Woonhuis.
Jet Ski Rental Conneaut Lake Pa
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Kcwi Tv Schedule
What Time Does Walmart Auto Center Open
Nesb Routing Number
Olivia Maeday
Random Bibleizer
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Black Lion Backpack And Glider Voucher
Gopher Carts Pensacola Beach
Duke University Transcript Request
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Jambus - Definition, Beispiele, Merkmale, Wirkung
Ark Unlock All Skins Command
Craigslist Red Wing Mn
D3 Boards
Jail View Sumter
Nancy Pazelt Obituary
Birmingham City Schools Clever Login
Thotsbook Com
Funkin' on the Heights
Vci Classified Paducah
Www Pig11 Net
Ty Glass Sentenced
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated:

Views: 5934

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.