/
usr
/
share
/
doc
/
python2-docs
/
html
/
library
/
/usr/share/doc/python2-docs/html/library
mkdir
upload
Name
Size
Mode
Actions
2to3.html
59507
0644
edit
dl
rm
abc.html
25826
0644
edit
dl
rm
aepack.html
14164
0644
edit
dl
rm
aetools.html
16389
0644
edit
dl
rm
aetypes.html
21021
0644
edit
dl
rm
aifc.html
25064
0644
edit
dl
rm
al.html
18672
0644
edit
dl
rm
allos.html
35297
0644
edit
dl
rm
anydbm.html
18124
0644
edit
dl
rm
archiving.html
10078
0644
edit
dl
rm
argparse.html
263616
0644
edit
dl
rm
array.html
31855
0644
edit
dl
rm
ast.html
38167
0644
edit
dl
rm
asynchat.html
33930
0644
edit
dl
rm
asyncore.html
40631
0644
edit
dl
rm
atexit.html
18455
0644
edit
dl
rm
audioop.html
34252
0644
edit
dl
rm
autogil.html
8733
0644
edit
dl
rm
base64.html
21862
0644
edit
dl
rm
basehttpserver.html
37441
0644
edit
dl
rm
bastion.html
11798
0644
edit
dl
rm
bdb.html
41395
0644
edit
dl
rm
binascii.html
22997
0644
edit
dl
rm
binhex.html
11309
0644
edit
dl
rm
bisect.html
24666
0644
edit
dl
rm
bsddb.html
28589
0644
edit
dl
rm
bz2.html
29048
0644
edit
dl
rm
calendar.html
41762
0644
edit
dl
rm
carbon.html
51998
0644
edit
dl
rm
cd.html
30033
0644
edit
dl
rm
cgi.html
55922
0644
edit
dl
rm
cgihttpserver.html
14026
0644
edit
dl
rm
cgitb.html
12263
0644
edit
dl
rm
chunk.html
15899
0644
edit
dl
rm
cmath.html
28365
0644
edit
dl
rm
cmd.html
29042
0644
edit
dl
rm
code.html
26959
0644
edit
dl
rm
codecs.html
118258
0644
edit
dl
rm
codeop.html
15898
0644
edit
dl
rm
collections.html
147647
0644
edit
dl
rm
colorpicker.html
8035
0644
edit
dl
rm
colorsys.html
11919
0644
edit
dl
rm
commands.html
15516
0644
edit
dl
rm
compileall.html
18621
0644
edit
dl
rm
compiler.html
75897
0644
edit
dl
rm
configparser.html
67751
0644
edit
dl
rm
constants.html
13979
0644
edit
dl
rm
contextlib.html
22777
0644
edit
dl
rm
cookie.html
41854
0644
edit
dl
rm
cookielib.html
91032
0644
edit
dl
rm
copy.html
13004
0644
edit
dl
rm
copy_reg.html
14691
0644
edit
dl
rm
crypt.html
10647
0644
edit
dl
rm
crypto.html
7761
0644
edit
dl
rm
csv.html
76456
0644
edit
dl
rm
ctypes.html
264575
0644
edit
dl
rm
curses.ascii.html
24873
0644
edit
dl
rm
curses.html
167501
0644
edit
dl
rm
curses.panel.html
15824
0644
edit
dl
rm
custominterp.html
8026
0644
edit
dl
rm
datatypes.html
18015
0644
edit
dl
rm
datetime.html
253438
0644
edit
dl
rm
dbhash.html
16825
0644
edit
dl
rm
dbm.html
13507
0644
edit
dl
rm
debug.html
10703
0644
edit
dl
rm
decimal.html
222315
0644
edit
dl
rm
development.html
14828
0644
edit
dl
rm
difflib.html
91975
0644
edit
dl
rm
dircache.html
12299
0644
edit
dl
rm
dis.html
84156
0644
edit
dl
rm
distribution.html
7679
0644
edit
dl
rm
distutils.html
10152
0644
edit
dl
rm
dl.html
17668
0644
edit
dl
rm
doctest.html
185883
0644
edit
dl
rm
docxmlrpcserver.html
17573
0644
edit
dl
rm
dumbdbm.html
15549
0644
edit
dl
rm
dummy_thread.html
10065
0644
edit
dl
rm
dummy_threading.html
8934
0644
edit
dl
rm
easydialogs.html
33124
0644
edit
dl
rm
email-examples.html
47424
0644
edit
dl
rm
email.charset.html
29424
0644
edit
dl
rm
email.encoders.html
12847
0644
edit
dl
rm
email.errors.html
17952
0644
edit
dl
rm
email.generator.html
23141
0644
edit
dl
rm
email.header.html
29108
0644
edit
dl
rm
email.html
55722
0644
edit
dl
rm
email.iterators.html
12623
0644
edit
dl
rm
email.message.html
70189
0644
edit
dl
rm
email.mime.html
31655
0644
edit
dl
rm
email.parser.html
35142
0644
edit
dl
rm
email.utils.html
27076
0644
edit
dl
rm
ensurepip.html
18327
0644
edit
dl
rm
errno.html
40246
0644
edit
dl
rm
exceptions.html
63693
0644
edit
dl
rm
fcntl.html
26245
0644
edit
dl
rm
filecmp.html
23997
0644
edit
dl
rm
fileformats.html
9653
0644
edit
dl
rm
fileinput.html
27071
0644
edit
dl
rm
filesys.html
10866
0644
edit
dl
rm
fl.html
56525
0644
edit
dl
rm
fm.html
13009
0644
edit
dl
rm
fnmatch.html
16258
0644
edit
dl
rm
formatter.html
37371
0644
edit
dl
rm
fpectl.html
16879
0644
edit
dl
rm
fpformat.html
11455
0644
edit
dl
rm
fractions.html
24888
0644
edit
dl
rm
framework.html
36934
0644
edit
dl
rm
frameworks.html
7551
0644
edit
dl
rm
ftplib.html
49577
0644
edit
dl
rm
functions.html
205640
0644
edit
dl
rm
functools.html
29645
0644
edit
dl
rm
future_builtins.html
14477
0644
edit
dl
rm
gc.html
28423
0644
edit
dl
rm
gdbm.html
17805
0644
edit
dl
rm
gensuitemodule.html
12574
0644
edit
dl
rm
getopt.html
25280
0644
edit
dl
rm
getpass.html
11426
0644
edit
dl
rm
gettext.html
84971
0644
edit
dl
rm
gl.html
24342
0644
edit
dl
rm
glob.html
14430
0644
edit
dl
rm
grp.html
11316
0644
edit
dl
rm
gzip.html
20576
0644
edit
dl
rm
hashlib.html
25467
0644
edit
dl
rm
heapq.html
34890
0644
edit
dl
rm
hmac.html
14374
0644
edit
dl
rm
hotshot.html
20147
0644
edit
dl
rm
htmllib.html
27682
0644
edit
dl
rm
htmlparser.html
42433
0644
edit
dl
rm
httplib.html
70932
0644
edit
dl
rm
i18n.html
10047
0644
edit
dl
rm
ic.html
18654
0644
edit
dl
rm
idle.html
42148
0644
edit
dl
rm
imageop.html
16099
0644
edit
dl
rm
imaplib.html
58515
0644
edit
dl
rm
imgfile.html
12732
0644
edit
dl
rm
imghdr.html
12238
0644
edit
dl
rm
imp.html
37603
0644
edit
dl
rm
importlib.html
8926
0644
edit
dl
rm
imputil.html
33563
0644
edit
dl
rm
index.html
79088
0644
edit
dl
rm
inspect.html
56823
0644
edit
dl
rm
internet.html
26138
0644
edit
dl
rm
intro.html
9353
0644
edit
dl
rm
io.html
113701
0644
edit
dl
rm
ipc.html
16598
0644
edit
dl
rm
itertools.html
125397
0644
edit
dl
rm
jpeg.html
13757
0644
edit
dl
rm
json.html
73678
0644
edit
dl
rm
keyword.html
8210
0644
edit
dl
rm
language.html
11687
0644
edit
dl
rm
linecache.html
11416
0644
edit
dl
rm
locale.html
61576
0644
edit
dl
rm
logging.config.html
80045
0644
edit
dl
rm
logging.handlers.html
80194
0644
edit
dl
rm
logging.html
110254
0644
edit
dl
rm
mac.html
23376
0644
edit
dl
rm
macos.html
16129
0644
edit
dl
rm
macosa.html
14080
0644
edit
dl
rm
macostools.html
16898
0644
edit
dl
rm
macpath.html
8386
0644
edit
dl
rm
mailbox.html
171121
0644
edit
dl
rm
mailcap.html
14132
0644
edit
dl
rm
markup.html
19862
0644
edit
dl
rm
marshal.html
19456
0644
edit
dl
rm
math.html
44015
0644
edit
dl
rm
md5.html
15123
0644
edit
dl
rm
mhlib.html
24123
0644
edit
dl
rm
mimetools.html
21206
0644
edit
dl
rm
mimetypes.html
30634
0644
edit
dl
rm
mimewriter.html
16078
0644
edit
dl
rm
mimify.html
15279
0644
edit
dl
rm
miniaeframe.html
13109
0644
edit
dl
rm
misc.html
7236
0644
edit
dl
rm
mm.html
9570
0644
edit
dl
rm
mmap.html
30832
0644
edit
dl
rm
modulefinder.html
18096
0644
edit
dl
rm
modules.html
9017
0644
edit
dl
rm
msilib.html
57853
0644
edit
dl
rm
msvcrt.html
21232
0644
edit
dl
rm
multifile.html
26256
0644
edit
dl
rm
multiprocessing.html
414535
0644
edit
dl
rm
mutex.html
12147
0644
edit
dl
rm
netdata.html
18379
0644
edit
dl
rm
netrc.html
14021
0644
edit
dl
rm
new.html
13177
0644
edit
dl
rm
nis.html
11502
0644
edit
dl
rm
nntplib.html
45689
0644
edit
dl
rm
numbers.html
40636
0644
edit
dl
rm
numeric.html
14292
0644
edit
dl
rm
operator.html
93622
0644
edit
dl
rm
optparse.html
250109
0644
edit
dl
rm
os.html
240195
0644
edit
dl
rm
os.path.html
43402
0644
edit
dl
rm
ossaudiodev.html
45594
0644
edit
dl
rm
othergui.html
9403
0644
edit
dl
rm
parser.html
42558
0644
edit
dl
rm
pdb.html
38603
0644
edit
dl
rm
persistence.html
15676
0644
edit
dl
rm
pickle.html
109684
0644
edit
dl
rm
pickletools.html
11475
0644
edit
dl
rm
pipes.html
19685
0644
edit
dl
rm
pkgutil.html
27367
0644
edit
dl
rm
platform.html
31599
0644
edit
dl
rm
plistlib.html
18344
0644
edit
dl
rm
popen2.html
27601
0644
edit
dl
rm
poplib.html
24320
0644
edit
dl
rm
posix.html
16626
0644
edit
dl
rm
posixfile.html
21387
0644
edit
dl
rm
pprint.html
32207
0644
edit
dl
rm
profile.html
72788
0644
edit
dl
rm
pty.html
10195
0644
edit
dl
rm
pwd.html
12388
0644
edit
dl
rm
pyclbr.html
15812
0644
edit
dl
rm
pydoc.html
13650
0644
edit
dl
rm
pyexpat.html
80886
0644
edit
dl
rm
python.html
12854
0644
edit
dl
rm
py_compile.html
11932
0644
edit
dl
rm
queue.html
26856
0644
edit
dl
rm
quopri.html
12739
0644
edit
dl
rm
random.html
42722
0644
edit
dl
rm
re.html
155985
0644
edit
dl
rm
readline.html
37383
0644
edit
dl
rm
repr.html
21778
0644
edit
dl
rm
resource.html
28286
0644
edit
dl
rm
restricted.html
12366
0644
edit
dl
rm
rexec.html
40566
0644
edit
dl
rm
rfc822.html
46408
0644
edit
dl
rm
rlcompleter.html
14460
0644
edit
dl
rm
robotparser.html
13403
0644
edit
dl
rm
runpy.html
21925
0644
edit
dl
rm
sched.html
19878
0644
edit
dl
rm
scrolledtext.html
9728
0644
edit
dl
rm
select.html
44090
0644
edit
dl
rm
sets.html
40151
0644
edit
dl
rm
sgi.html
10371
0644
edit
dl
rm
sgmllib.html
34464
0644
edit
dl
rm
sha.html
13062
0644
edit
dl
rm
shelve.html
29955
0644
edit
dl
rm
shlex.html
35033
0644
edit
dl
rm
shutil.html
45454
0644
edit
dl
rm
signal.html
34023
0644
edit
dl
rm
simplehttpserver.html
20351
0644
edit
dl
rm
simplexmlrpcserver.html
37794
0644
edit
dl
rm
site.html
26887
0644
edit
dl
rm
smtpd.html
13587
0644
edit
dl
rm
smtplib.html
47054
0644
edit
dl
rm
sndhdr.html
10993
0644
edit
dl
rm
socket.html
116414
0644
edit
dl
rm
socketserver.html
76430
0644
edit
dl
rm
someos.html
16472
0644
edit
dl
rm
spwd.html
11163
0644
edit
dl
rm
sqlite3.html
150014
0644
edit
dl
rm
ssl.html
202453
0644
edit
dl
rm
stat.html
34597
0644
edit
dl
rm
statvfs.html
11326
0644
edit
dl
rm
stdtypes.html
298144
0644
edit
dl
rm
string.html
120219
0644
edit
dl
rm
stringio.html
20061
0644
edit
dl
rm
stringprep.html
17729
0644
edit
dl
rm
strings.html
15905
0644
edit
dl
rm
struct.html
44896
0644
edit
dl
rm
subprocess.html
110447
0644
edit
dl
rm
sun.html
7253
0644
edit
dl
rm
sunau.html
30011
0644
edit
dl
rm
sunaudio.html
19236
0644
edit
dl
rm
symbol.html
8140
0644
edit
dl
rm
symtable.html
25871
0644
edit
dl
rm
sys.html
110996
0644
edit
dl
rm
sysconfig.html
26299
0644
edit
dl
rm
syslog.html
19737
0644
edit
dl
rm
tabnanny.html
11393
0644
edit
dl
rm
tarfile.html
88728
0644
edit
dl
rm
telnetlib.html
27782
0644
edit
dl
rm
tempfile.html
31903
0644
edit
dl
rm
termios.html
17309
0644
edit
dl
rm
test.html
57030
0644
edit
dl
rm
textwrap.html
30135
0644
edit
dl
rm
thread.html
21486
0644
edit
dl
rm
threading.html
86655
0644
edit
dl
rm
time.html
63798
0644
edit
dl
rm
timeit.html
40529
0644
edit
dl
rm
tix.html
50582
0644
edit
dl
rm
tk.html
26568
0644
edit
dl
rm
tkinter.html
84316
0644
edit
dl
rm
token.html
20999
0644
edit
dl
rm
tokenize.html
20607
0644
edit
dl
rm
trace.html
28668
0644
edit
dl
rm
traceback.html
40900
0644
edit
dl
rm
ttk.html
108515
0644
edit
dl
rm
tty.html
9753
0644
edit
dl
rm
turtle.html
230483
0644
edit
dl
rm
types.html
29833
0644
edit
dl
rm
undoc.html
24680
0644
edit
dl
rm
unicodedata.html
20186
0644
edit
dl
rm
unittest.html
225021
0644
edit
dl
rm
unix.html
11223
0644
edit
dl
rm
urllib.html
68266
0644
edit
dl
rm
urllib2.html
113380
0644
edit
dl
rm
urlparse.html
43474
0644
edit
dl
rm
user.html
12707
0644
edit
dl
rm
userdict.html
32069
0644
edit
dl
rm
uu.html
11810
0644
edit
dl
rm
uuid.html
30137
0644
edit
dl
rm
warnings.html
50950
0644
edit
dl
rm
wave.html
24894
0644
edit
dl
rm
weakref.html
38808
0644
edit
dl
rm
webbrowser.html
26565
0644
edit
dl
rm
whichdb.html
9499
0644
edit
dl
rm
windows.html
9817
0644
edit
dl
rm
winsound.html
20466
0644
edit
dl
rm
wsgiref.html
88384
0644
edit
dl
rm
xdrlib.html
33018
0644
edit
dl
rm
xml.dom.html
97877
0644
edit
dl
rm
xml.dom.minidom.html
43230
0644
edit
dl
rm
xml.dom.pulldom.html
13835
0644
edit
dl
rm
xml.etree.elementtree.html
111804
0644
edit
dl
rm
xml.html
18007
0644
edit
dl
rm
xml.sax.handler.html
41877
0644
edit
dl
rm
xml.sax.html
23458
0644
edit
dl
rm
xml.sax.reader.html
44802
0644
edit
dl
rm
xml.sax.utils.html
16087
0644
edit
dl
rm
xmlrpclib.html
69191
0644
edit
dl
rm
zipfile.html
62227
0644
edit
dl
rm
zipimport.html
22769
0644
edit
dl
rm
zlib.html
31230
0644
edit
dl
rm
_winreg.html
64631
0644
edit
dl
rm
__builtin__.html
11069
0644
edit
dl
rm
__future__.html
14688
0644
edit
dl
rm
__main__.html
7512
0644
edit
dl
rm
Edit:
/usr/share/doc/python2-docs/html/library/string.html
(120219B)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>7.1. string — Common string operations — Python 2.7.16 documentation</title> <link rel="stylesheet" href="../_static/classic.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.16 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="7.2. re — Regular expression operations" href="re.html" /> <link rel="prev" title="7. String Services" href="strings.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/2/library/string.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="re.html" title="7.2. re — Regular expression operations" accesskey="N">next</a> |</li> <li class="right" > <a href="strings.html" title="7. String Services" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="strings.html" accesskey="U">7. String Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-string"> <span id="string-common-string-operations"></span><h1>7.1. <a class="reference internal" href="#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> — Common string operations<a class="headerlink" href="#module-string" title="Permalink to this headline">¶</a></h1> <p id="index-0"><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/2.7/Lib/string.py">Lib/string.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module contains a number of useful constants and classes, as well as some deprecated legacy functions that are also available as methods on strings. In addition, Python’s built-in string classes support the sequence type methods described in the <a class="reference internal" href="stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</span></a> section, and also the string-specific methods described in the <a class="reference internal" href="stdtypes.html#string-methods"><span class="std std-ref">String Methods</span></a> section. To output formatted strings use template strings or the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator described in the <a class="reference internal" href="stdtypes.html#string-formatting"><span class="std std-ref">String Formatting Operations</span></a> section. Also, see the <a class="reference internal" href="re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module for string functions based on regular expressions.</p> <div class="section" id="string-constants"> <h2>7.1.1. String constants<a class="headerlink" href="#string-constants" title="Permalink to this headline">¶</a></h2> <p>The constants defined in this module are:</p> <dl class="data"> <dt id="string.ascii_letters"> <code class="descclassname">string.</code><code class="descname">ascii_letters</code><a class="headerlink" href="#string.ascii_letters" title="Permalink to this definition">¶</a></dt> <dd><p>The concatenation of the <a class="reference internal" href="#string.ascii_lowercase" title="string.ascii_lowercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_lowercase</span></code></a> and <a class="reference internal" href="#string.ascii_uppercase" title="string.ascii_uppercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_uppercase</span></code></a> constants described below. This value is not locale-dependent.</p> </dd></dl> <dl class="data"> <dt id="string.ascii_lowercase"> <code class="descclassname">string.</code><code class="descname">ascii_lowercase</code><a class="headerlink" href="#string.ascii_lowercase" title="Permalink to this definition">¶</a></dt> <dd><p>The lowercase letters <code class="docutils literal notranslate"><span class="pre">'abcdefghijklmnopqrstuvwxyz'</span></code>. This value is not locale-dependent and will not change.</p> </dd></dl> <dl class="data"> <dt id="string.ascii_uppercase"> <code class="descclassname">string.</code><code class="descname">ascii_uppercase</code><a class="headerlink" href="#string.ascii_uppercase" title="Permalink to this definition">¶</a></dt> <dd><p>The uppercase letters <code class="docutils literal notranslate"><span class="pre">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span></code>. This value is not locale-dependent and will not change.</p> </dd></dl> <dl class="data"> <dt id="string.digits"> <code class="descclassname">string.</code><code class="descname">digits</code><a class="headerlink" href="#string.digits" title="Permalink to this definition">¶</a></dt> <dd><p>The string <code class="docutils literal notranslate"><span class="pre">'0123456789'</span></code>.</p> </dd></dl> <dl class="data"> <dt id="string.hexdigits"> <code class="descclassname">string.</code><code class="descname">hexdigits</code><a class="headerlink" href="#string.hexdigits" title="Permalink to this definition">¶</a></dt> <dd><p>The string <code class="docutils literal notranslate"><span class="pre">'0123456789abcdefABCDEF'</span></code>.</p> </dd></dl> <dl class="data"> <dt id="string.letters"> <code class="descclassname">string.</code><code class="descname">letters</code><a class="headerlink" href="#string.letters" title="Permalink to this definition">¶</a></dt> <dd><p>The concatenation of the strings <a class="reference internal" href="#string.lowercase" title="string.lowercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">lowercase</span></code></a> and <a class="reference internal" href="#string.uppercase" title="string.uppercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">uppercase</span></code></a> described below. The specific value is locale-dependent, and will be updated when <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.setlocale()</span></code></a> is called.</p> </dd></dl> <dl class="data"> <dt id="string.lowercase"> <code class="descclassname">string.</code><code class="descname">lowercase</code><a class="headerlink" href="#string.lowercase" title="Permalink to this definition">¶</a></dt> <dd><p>A string containing all the characters that are considered lowercase letters. On most systems this is the string <code class="docutils literal notranslate"><span class="pre">'abcdefghijklmnopqrstuvwxyz'</span></code>. The specific value is locale-dependent, and will be updated when <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.setlocale()</span></code></a> is called.</p> </dd></dl> <dl class="data"> <dt id="string.octdigits"> <code class="descclassname">string.</code><code class="descname">octdigits</code><a class="headerlink" href="#string.octdigits" title="Permalink to this definition">¶</a></dt> <dd><p>The string <code class="docutils literal notranslate"><span class="pre">'01234567'</span></code>.</p> </dd></dl> <dl class="data"> <dt id="string.punctuation"> <code class="descclassname">string.</code><code class="descname">punctuation</code><a class="headerlink" href="#string.punctuation" title="Permalink to this definition">¶</a></dt> <dd><p>String of ASCII characters which are considered punctuation characters in the <code class="docutils literal notranslate"><span class="pre">C</span></code> locale.</p> </dd></dl> <dl class="data"> <dt id="string.printable"> <code class="descclassname">string.</code><code class="descname">printable</code><a class="headerlink" href="#string.printable" title="Permalink to this definition">¶</a></dt> <dd><p>String of characters which are considered printable. This is a combination of <a class="reference internal" href="#string.digits" title="string.digits"><code class="xref py py-const docutils literal notranslate"><span class="pre">digits</span></code></a>, <a class="reference internal" href="#string.letters" title="string.letters"><code class="xref py py-const docutils literal notranslate"><span class="pre">letters</span></code></a>, <a class="reference internal" href="#string.punctuation" title="string.punctuation"><code class="xref py py-const docutils literal notranslate"><span class="pre">punctuation</span></code></a>, and <a class="reference internal" href="#string.whitespace" title="string.whitespace"><code class="xref py py-const docutils literal notranslate"><span class="pre">whitespace</span></code></a>.</p> </dd></dl> <dl class="data"> <dt id="string.uppercase"> <code class="descclassname">string.</code><code class="descname">uppercase</code><a class="headerlink" href="#string.uppercase" title="Permalink to this definition">¶</a></dt> <dd><p>A string containing all the characters that are considered uppercase letters. On most systems this is the string <code class="docutils literal notranslate"><span class="pre">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span></code>. The specific value is locale-dependent, and will be updated when <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.setlocale()</span></code></a> is called.</p> </dd></dl> <dl class="data"> <dt id="string.whitespace"> <code class="descclassname">string.</code><code class="descname">whitespace</code><a class="headerlink" href="#string.whitespace" title="Permalink to this definition">¶</a></dt> <dd><p>A string containing all characters that are considered whitespace. On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab.</p> </dd></dl> </div> <div class="section" id="custom-string-formatting"> <span id="new-string-formatting"></span><h2>7.1.2. Custom String Formatting<a class="headerlink" href="#custom-string-formatting" title="Permalink to this headline">¶</a></h2> <div class="versionadded"> <p><span class="versionmodified">New in version 2.6.</span></p> </div> <p>The built-in str and unicode classes provide the ability to do complex variable substitutions and value formatting via the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method described in <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>. The <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> class in the <a class="reference internal" href="#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module allows you to create and customize your own string formatting behaviors using the same implementation as the built-in <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method.</p> <dl class="class"> <dt id="string.Formatter"> <em class="property">class </em><code class="descclassname">string.</code><code class="descname">Formatter</code><a class="headerlink" href="#string.Formatter" title="Permalink to this definition">¶</a></dt> <dd><p>The <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> class has the following public methods:</p> <dl class="method"> <dt id="string.Formatter.format"> <code class="descname">format</code><span class="sig-paren">(</span><em>format_string</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.format" title="Permalink to this definition">¶</a></dt> <dd><p>The primary API method. It takes a format string and an arbitrary set of positional and keyword arguments. It is just a wrapper that calls <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="string.Formatter.vformat"> <code class="descname">vformat</code><span class="sig-paren">(</span><em>format_string</em>, <em>args</em>, <em>kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.vformat" title="Permalink to this definition">¶</a></dt> <dd><p>This function does the actual work of formatting. It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the <code class="docutils literal notranslate"><span class="pre">*args</span></code> and <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> syntax. <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a> does the work of breaking up the format string into character data and replacement fields. It calls the various methods described below.</p> </dd></dl> <p>In addition, the <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> defines a number of methods that are intended to be replaced by subclasses:</p> <dl class="method"> <dt id="string.Formatter.parse"> <code class="descname">parse</code><span class="sig-paren">(</span><em>format_string</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.parse" title="Permalink to this definition">¶</a></dt> <dd><p>Loop over the format_string and return an iterable of tuples (<em>literal_text</em>, <em>field_name</em>, <em>format_spec</em>, <em>conversion</em>). This is used by <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a> to break the string into either literal text, or replacement fields.</p> <p>The values in the tuple conceptually represent a span of literal text followed by a single replacement field. If there is no literal text (which can happen if two replacement fields occur consecutively), then <em>literal_text</em> will be a zero-length string. If there is no replacement field, then the values of <em>field_name</em>, <em>format_spec</em> and <em>conversion</em> will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </dd></dl> <dl class="method"> <dt id="string.Formatter.get_field"> <code class="descname">get_field</code><span class="sig-paren">(</span><em>field_name</em>, <em>args</em>, <em>kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.get_field" title="Permalink to this definition">¶</a></dt> <dd><p>Given <em>field_name</em> as returned by <a class="reference internal" href="#string.Formatter.parse" title="string.Formatter.parse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse()</span></code></a> (see above), convert it to an object to be formatted. Returns a tuple (obj, used_key). The default version takes strings of the form defined in <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>, such as “0[name]” or “label.title”. <em>args</em> and <em>kwargs</em> are as passed in to <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a>. The return value <em>used_key</em> has the same meaning as the <em>key</em> parameter to <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_value()</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="string.Formatter.get_value"> <code class="descname">get_value</code><span class="sig-paren">(</span><em>key</em>, <em>args</em>, <em>kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.get_value" title="Permalink to this definition">¶</a></dt> <dd><p>Retrieve a given field value. The <em>key</em> argument will be either an integer or a string. If it is an integer, it represents the index of the positional argument in <em>args</em>; if it is a string, then it represents a named argument in <em>kwargs</em>.</p> <p>The <em>args</em> parameter is set to the list of positional arguments to <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a>, and the <em>kwargs</em> parameter is set to the dictionary of keyword arguments.</p> <p>For compound field names, these functions are only called for the first component of the field name; Subsequent components are handled through normal attribute and indexing operations.</p> <p>So for example, the field expression ‘0.name’ would cause <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_value()</span></code></a> to be called with a <em>key</em> argument of 0. The <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute will be looked up after <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_value()</span></code></a> returns by calling the built-in <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> function.</p> <p>If the index or keyword refers to an item that does not exist, then an <a class="reference internal" href="exceptions.html#exceptions.IndexError" title="exceptions.IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a> or <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> should be raised.</p> </dd></dl> <dl class="method"> <dt id="string.Formatter.check_unused_args"> <code class="descname">check_unused_args</code><span class="sig-paren">(</span><em>used_args</em>, <em>args</em>, <em>kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.check_unused_args" title="Permalink to this definition">¶</a></dt> <dd><p>Implement checking for unused arguments if desired. The arguments to this function is the set of all argument keys that were actually referred to in the format string (integers for positional arguments, and strings for named arguments), and a reference to the <em>args</em> and <em>kwargs</em> that was passed to vformat. The set of unused args can be calculated from these parameters. <a class="reference internal" href="#string.Formatter.check_unused_args" title="string.Formatter.check_unused_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">check_unused_args()</span></code></a> is assumed to raise an exception if the check fails.</p> </dd></dl> <dl class="method"> <dt id="string.Formatter.format_field"> <code class="descname">format_field</code><span class="sig-paren">(</span><em>value</em>, <em>format_spec</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.format_field" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#string.Formatter.format_field" title="string.Formatter.format_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format_field()</span></code></a> simply calls the global <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> built-in. The method is provided so that subclasses can override it.</p> </dd></dl> <dl class="method"> <dt id="string.Formatter.convert_field"> <code class="descname">convert_field</code><span class="sig-paren">(</span><em>value</em>, <em>conversion</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.convert_field" title="Permalink to this definition">¶</a></dt> <dd><p>Converts the value (returned by <a class="reference internal" href="#string.Formatter.get_field" title="string.Formatter.get_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_field()</span></code></a>) given a conversion type (as in the tuple returned by the <a class="reference internal" href="#string.Formatter.parse" title="string.Formatter.parse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse()</span></code></a> method). The default version understands ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii) conversion types.</p> </dd></dl> </dd></dl> </div> <div class="section" id="format-string-syntax"> <span id="formatstrings"></span><h2>7.1.3. Format String Syntax<a class="headerlink" href="#format-string-syntax" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method and the <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> class share the same syntax for format strings (although in the case of <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a>, subclasses can define their own format string syntax).</p> <p>Format strings contain “replacement fields” surrounded by curly braces <code class="docutils literal notranslate"><span class="pre">{}</span></code>. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: <code class="docutils literal notranslate"><span class="pre">{{</span></code> and <code class="docutils literal notranslate"><span class="pre">}}</span></code>.</p> <p>The grammar for a replacement field is as follows:</p> <blockquote> <div><pre> <strong id="grammar-token-replacement_field">replacement_field</strong> ::= "{" [<a class="reference internal" href="#grammar-token-field_name"><code class="xref docutils literal notranslate"><span class="pre">field_name</span></code></a>] ["!" <a class="reference internal" href="#grammar-token-conversion"><code class="xref docutils literal notranslate"><span class="pre">conversion</span></code></a>] [":" <a class="reference internal" href="#grammar-token-format_spec"><code class="xref docutils literal notranslate"><span class="pre">format_spec</span></code></a>] "}" <strong id="grammar-token-field_name">field_name </strong> ::= arg_name ("." <a class="reference internal" href="#grammar-token-attribute_name"><code class="xref docutils literal notranslate"><span class="pre">attribute_name</span></code></a> | "[" <a class="reference internal" href="#grammar-token-element_index"><code class="xref docutils literal notranslate"><span class="pre">element_index</span></code></a> "]")* <strong id="grammar-token-arg_name">arg_name </strong> ::= [<a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> | <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a>] <strong id="grammar-token-attribute_name">attribute_name </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> <strong id="grammar-token-element_index">element_index </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | <a class="reference internal" href="#grammar-token-index_string"><code class="xref docutils literal notranslate"><span class="pre">index_string</span></code></a> <strong id="grammar-token-index_string">index_string </strong> ::= <any source character except "]"> + <strong id="grammar-token-conversion">conversion </strong> ::= "r" | "s" <strong id="grammar-token-format_spec">format_spec </strong> ::= <described in the next section> </pre> </div></blockquote> <p>In less formal terms, the replacement field can start with a <em>field_name</em> that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. The <em>field_name</em> is optionally followed by a <em>conversion</em> field, which is preceded by an exclamation point <code class="docutils literal notranslate"><span class="pre">'!'</span></code>, and a <em>format_spec</em>, which is preceded by a colon <code class="docutils literal notranslate"><span class="pre">':'</span></code>. These specify a non-default format for the replacement value.</p> <p>See also the <a class="reference internal" href="#formatspec"><span class="std std-ref">Format Specification Mini-Language</span></a> section.</p> <p>The <em>field_name</em> itself begins with an <em>arg_name</em> that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. Because <em>arg_name</em> is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings <code class="docutils literal notranslate"><span class="pre">'10'</span></code> or <code class="docutils literal notranslate"><span class="pre">':-]'</span></code>) within a format string. The <em>arg_name</em> can be followed by any number of index or attribute expressions. An expression of the form <code class="docutils literal notranslate"><span class="pre">'.name'</span></code> selects the named attribute using <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a>, while an expression of the form <code class="docutils literal notranslate"><span class="pre">'[index]'</span></code> does an index lookup using <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__getitem__()</span></code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.7: </span>The positional argument specifiers can be omitted for <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">unicode.format()</span></code>, so <code class="docutils literal notranslate"><span class="pre">'{}</span> <span class="pre">{}'</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">'{0}</span> <span class="pre">{1}'</span></code>, <code class="docutils literal notranslate"><span class="pre">u'{}</span> <span class="pre">{}'</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">u'{0}</span> <span class="pre">{1}'</span></code>.</p> </div> <p>Some simple format string examples:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="s2">"First, thou shalt count to </span><span class="si">{0}</span><span class="s2">"</span> <span class="c1"># References first positional argument</span> <span class="s2">"Bring me a </span><span class="si">{}</span><span class="s2">"</span> <span class="c1"># Implicitly references the first positional argument</span> <span class="s2">"From </span><span class="si">{}</span><span class="s2"> to </span><span class="si">{}</span><span class="s2">"</span> <span class="c1"># Same as "From {0} to {1}"</span> <span class="s2">"My quest is </span><span class="si">{name}</span><span class="s2">"</span> <span class="c1"># References keyword argument 'name'</span> <span class="s2">"Weight in tons </span><span class="si">{0.weight}</span><span class="s2">"</span> <span class="c1"># 'weight' attribute of first positional arg</span> <span class="s2">"Units destroyed: </span><span class="si">{players[0]}</span><span class="s2">"</span> <span class="c1"># First element of keyword argument 'players'.</span> </pre></div> </div> <p>The <em>conversion</em> field causes a type coercion before formatting. Normally, the job of formatting a value is done by the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__format__()</span></code> method of the value itself. However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. By converting the value to a string before calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">__format__()</span></code>, the normal formatting logic is bypassed.</p> <p>Two conversion flags are currently supported: <code class="docutils literal notranslate"><span class="pre">'!s'</span></code> which calls <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> on the value, and <code class="docutils literal notranslate"><span class="pre">'!r'</span></code> which calls <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a>.</p> <p>Some examples:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="s2">"Harold's a clever </span><span class="si">{0!s}</span><span class="s2">"</span> <span class="c1"># Calls str() on the argument first</span> <span class="s2">"Bring out the holy </span><span class="si">{name!r}</span><span class="s2">"</span> <span class="c1"># Calls repr() on the argument first</span> </pre></div> </div> <p>The <em>format_spec</em> field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. Each value type can define its own “formatting mini-language” or interpretation of the <em>format_spec</em>.</p> <p>Most built-in types support a common formatting mini-language, which is described in the next section.</p> <p>A <em>format_spec</em> field can also include nested replacement fields within it. These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. The replacement fields within the format_spec are substituted before the <em>format_spec</em> string is interpreted. This allows the formatting of a value to be dynamically specified.</p> <p>See the <a class="reference internal" href="#formatexamples"><span class="std std-ref">Format examples</span></a> section for some examples.</p> <div class="section" id="format-specification-mini-language"> <span id="formatspec"></span><h3>7.1.3.1. Format Specification Mini-Language<a class="headerlink" href="#format-specification-mini-language" title="Permalink to this headline">¶</a></h3> <p>“Format specifications” are used within replacement fields contained within a format string to define how individual values are presented (see <a class="reference internal" href="#formatstrings"><span class="std std-ref">Format String Syntax</span></a>). They can also be passed directly to the built-in <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> function. Each formattable type may define how the format specification is to be interpreted.</p> <p>Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types.</p> <p>A general convention is that an empty format string (<code class="docutils literal notranslate"><span class="pre">""</span></code>) produces the same result as if you had called <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> on the value. A non-empty format string typically modifies the result.</p> <p>The general form of a <em>standard format specifier</em> is:</p> <pre> <strong id="id1">format_spec</strong> ::= [[<a class="reference internal" href="#grammar-token-fill"><code class="xref docutils literal notranslate"><span class="pre">fill</span></code></a>]<a class="reference internal" href="#grammar-token-align"><code class="xref docutils literal notranslate"><span class="pre">align</span></code></a>][<a class="reference internal" href="#grammar-token-sign"><code class="xref docutils literal notranslate"><span class="pre">sign</span></code></a>][#][0][<a class="reference internal" href="#grammar-token-width"><code class="xref docutils literal notranslate"><span class="pre">width</span></code></a>][,][.<a class="reference internal" href="#grammar-token-precision"><code class="xref docutils literal notranslate"><span class="pre">precision</span></code></a>][<a class="reference internal" href="#grammar-token-type"><code class="xref docutils literal notranslate"><span class="pre">type</span></code></a>] <strong id="grammar-token-fill">fill </strong> ::= <any character> <strong id="grammar-token-align">align </strong> ::= "<" | ">" | "=" | "^" <strong id="grammar-token-sign">sign </strong> ::= "+" | "-" | " " <strong id="grammar-token-width">width </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> <strong id="grammar-token-precision">precision </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> <strong id="grammar-token-type">type </strong> ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" </pre> <p>If a valid <em>align</em> value is specified, it can be preceded by a <em>fill</em> character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (“<code class="docutils literal notranslate"><span class="pre">{</span></code>” or “<code class="docutils literal notranslate"><span class="pre">}</span></code>”) as the <em>fill</em> character when using the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesn’t affect the <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> function.</p> <p>The meaning of the various alignment options is as follows:</p> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="13%" /> <col width="87%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'<'</span></code></td> <td>Forces the field to be left-aligned within the available space (this is the default for most objects).</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'>'</span></code></td> <td>Forces the field to be right-aligned within the available space (this is the default for numbers).</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'='</span></code></td> <td>Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types. It becomes the default when ‘0’ immediately precedes the field width.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'^'</span></code></td> <td>Forces the field to be centered within the available space.</td> </tr> </tbody> </table> </div></blockquote> <p>Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case.</p> <p>The <em>sign</em> option is only valid for number types, and can be one of the following:</p> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="13%" /> <col width="87%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Option</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'+'</span></code></td> <td>indicates that a sign should be used for both positive as well as negative numbers.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'-'</span></code></td> <td>indicates that a sign should be used only for negative numbers (this is the default behavior).</td> </tr> <tr class="row-even"><td>space</td> <td>indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers.</td> </tr> </tbody> </table> </div></blockquote> <p>The <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option is only valid for integers, and only for binary, octal, or hexadecimal output. If present, it specifies that the output will be prefixed by <code class="docutils literal notranslate"><span class="pre">'0b'</span></code>, <code class="docutils literal notranslate"><span class="pre">'0o'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'0x'</span></code>, respectively.</p> <p>The <code class="docutils literal notranslate"><span class="pre">','</span></code> option signals the use of a comma for a thousands separator. For a locale aware separator, use the <code class="docutils literal notranslate"><span class="pre">'n'</span></code> integer presentation type instead.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.7: </span>Added the <code class="docutils literal notranslate"><span class="pre">','</span></code> option (see also <span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0378"><strong>PEP 378</strong></a>).</p> </div> <p><em>width</em> is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.</p> <p>When no explicit alignment is given, preceding the <em>width</em> field by a zero (<code class="docutils literal notranslate"><span class="pre">'0'</span></code>) character enables sign-aware zero-padding for numeric types. This is equivalent to a <em>fill</em> character of <code class="docutils literal notranslate"><span class="pre">'0'</span></code> with an <em>alignment</em> type of <code class="docutils literal notranslate"><span class="pre">'='</span></code>.</p> <p>The <em>precision</em> is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with <code class="docutils literal notranslate"><span class="pre">'f'</span></code> and <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, or before and after the decimal point for a floating point value formatted with <code class="docutils literal notranslate"><span class="pre">'g'</span></code> or <code class="docutils literal notranslate"><span class="pre">'G'</span></code>. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The <em>precision</em> is not allowed for integer values.</p> <p>Finally, the <em>type</em> determines how the data should be presented.</p> <p>The available string presentation types are:</p> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="13%" /> <col width="87%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Type</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'s'</span></code></td> <td>String format. This is the default type for strings and may be omitted.</td> </tr> <tr class="row-odd"><td>None</td> <td>The same as <code class="docutils literal notranslate"><span class="pre">'s'</span></code>.</td> </tr> </tbody> </table> </div></blockquote> <p>The available integer presentation types are:</p> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="13%" /> <col width="87%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Type</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'b'</span></code></td> <td>Binary format. Outputs the number in base 2.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'c'</span></code></td> <td>Character. Converts the integer to the corresponding unicode character before printing.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'d'</span></code></td> <td>Decimal Integer. Outputs the number in base 10.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'o'</span></code></td> <td>Octal format. Outputs the number in base 8.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'x'</span></code></td> <td>Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'X'</span></code></td> <td>Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'n'</span></code></td> <td>Number. This is the same as <code class="docutils literal notranslate"><span class="pre">'d'</span></code>, except that it uses the current locale setting to insert the appropriate number separator characters.</td> </tr> <tr class="row-odd"><td>None</td> <td>The same as <code class="docutils literal notranslate"><span class="pre">'d'</span></code>.</td> </tr> </tbody> </table> </div></blockquote> <p>In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except <code class="docutils literal notranslate"><span class="pre">'n'</span></code> and <code class="docutils literal notranslate"><span class="pre">None</span></code>). When doing so, <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a> is used to convert the integer to a floating point number before formatting.</p> <p>The available presentation types for floating point and decimal values are:</p> <blockquote> <div><table border="1" class="docutils"> <colgroup> <col width="13%" /> <col width="87%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Type</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'e'</span></code></td> <td>Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is <code class="docutils literal notranslate"><span class="pre">6</span></code>.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'E'</span></code></td> <td>Exponent notation. Same as <code class="docutils literal notranslate"><span class="pre">'e'</span></code> except it uses an upper case ‘E’ as the separator character.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'f'</span></code></td> <td>Fixed-point notation. Displays the number as a fixed-point number. The default precision is <code class="docutils literal notranslate"><span class="pre">6</span></code>.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'F'</span></code></td> <td>Fixed point notation. Same as <code class="docutils literal notranslate"><span class="pre">'f'</span></code>.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'g'</span></code></td> <td><p class="first">General format. For a given precision <code class="docutils literal notranslate"><span class="pre">p</span> <span class="pre">>=</span> <span class="pre">1</span></code>, this rounds the number to <code class="docutils literal notranslate"><span class="pre">p</span></code> significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.</p> <p>The precise rules are as follows: suppose that the result formatted with presentation type <code class="docutils literal notranslate"><span class="pre">'e'</span></code> and precision <code class="docutils literal notranslate"><span class="pre">p-1</span></code> would have exponent <code class="docutils literal notranslate"><span class="pre">exp</span></code>. Then if <code class="docutils literal notranslate"><span class="pre">-4</span> <span class="pre"><=</span> <span class="pre">exp</span> <span class="pre"><</span> <span class="pre">p</span></code>, the number is formatted with presentation type <code class="docutils literal notranslate"><span class="pre">'f'</span></code> and precision <code class="docutils literal notranslate"><span class="pre">p-1-exp</span></code>. Otherwise, the number is formatted with presentation type <code class="docutils literal notranslate"><span class="pre">'e'</span></code> and precision <code class="docutils literal notranslate"><span class="pre">p-1</span></code>. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.</p> <p>Positive and negative infinity, positive and negative zero, and nans, are formatted as <code class="docutils literal notranslate"><span class="pre">inf</span></code>, <code class="docutils literal notranslate"><span class="pre">-inf</span></code>, <code class="docutils literal notranslate"><span class="pre">0</span></code>, <code class="docutils literal notranslate"><span class="pre">-0</span></code> and <code class="docutils literal notranslate"><span class="pre">nan</span></code> respectively, regardless of the precision.</p> <p class="last">A precision of <code class="docutils literal notranslate"><span class="pre">0</span></code> is treated as equivalent to a precision of <code class="docutils literal notranslate"><span class="pre">1</span></code>. The default precision is <code class="docutils literal notranslate"><span class="pre">6</span></code>.</p> </td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'G'</span></code></td> <td>General format. Same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code> except switches to <code class="docutils literal notranslate"><span class="pre">'E'</span></code> if the number gets too large. The representations of infinity and NaN are uppercased, too.</td> </tr> <tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'n'</span></code></td> <td>Number. This is the same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code>, except that it uses the current locale setting to insert the appropriate number separator characters.</td> </tr> <tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'%'</span></code></td> <td>Percentage. Multiplies the number by 100 and displays in fixed (<code class="docutils literal notranslate"><span class="pre">'f'</span></code>) format, followed by a percent sign.</td> </tr> <tr class="row-even"><td>None</td> <td>The same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code>.</td> </tr> </tbody> </table> </div></blockquote> </div> <div class="section" id="format-examples"> <span id="formatexamples"></span><h3>7.1.3.2. Format examples<a class="headerlink" href="#format-examples" title="Permalink to this headline">¶</a></h3> <p>This section contains examples of the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> syntax and comparison with the old <code class="docutils literal notranslate"><span class="pre">%</span></code>-formatting.</p> <p>In most of the cases the syntax is similar to the old <code class="docutils literal notranslate"><span class="pre">%</span></code>-formatting, with the addition of the <code class="docutils literal notranslate"><span class="pre">{}</span></code> and with <code class="docutils literal notranslate"><span class="pre">:</span></code> used instead of <code class="docutils literal notranslate"><span class="pre">%</span></code>. For example, <code class="docutils literal notranslate"><span class="pre">'%03.2f'</span></code> can be translated to <code class="docutils literal notranslate"><span class="pre">'{:03.2f}'</span></code>.</p> <p>The new format syntax also supports new and different options, shown in the following examples.</p> <p>Accessing arguments by position:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="si">{0}</span><span class="s1">, </span><span class="si">{1}</span><span class="s1">, </span><span class="si">{2}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">)</span> <span class="go">'a, b, c'</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">)</span> <span class="c1"># 2.7+ only</span> <span class="go">'a, b, c'</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{2}</span><span class="s1">, </span><span class="si">{1}</span><span class="s1">, </span><span class="si">{0}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">)</span> <span class="go">'c, b, a'</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{2}</span><span class="s1">, </span><span class="si">{1}</span><span class="s1">, </span><span class="si">{0}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="s1">'abc'</span><span class="p">)</span> <span class="c1"># unpacking argument sequence</span> <span class="go">'c, b, a'</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{0}{1}{0}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'abra'</span><span class="p">,</span> <span class="s1">'cad'</span><span class="p">)</span> <span class="c1"># arguments' indices can be repeated</span> <span class="go">'abracadabra'</span> </pre></div> </div> <p>Accessing arguments by name:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'Coordinates: </span><span class="si">{latitude}</span><span class="s1">, </span><span class="si">{longitude}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">latitude</span><span class="o">=</span><span class="s1">'37.24N'</span><span class="p">,</span> <span class="n">longitude</span><span class="o">=</span><span class="s1">'-115.81W'</span><span class="p">)</span> <span class="go">'Coordinates: 37.24N, -115.81W'</span> <span class="gp">>>> </span><span class="n">coord</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'latitude'</span><span class="p">:</span> <span class="s1">'37.24N'</span><span class="p">,</span> <span class="s1">'longitude'</span><span class="p">:</span> <span class="s1">'-115.81W'</span><span class="p">}</span> <span class="gp">>>> </span><span class="s1">'Coordinates: </span><span class="si">{latitude}</span><span class="s1">, </span><span class="si">{longitude}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">**</span><span class="n">coord</span><span class="p">)</span> <span class="go">'Coordinates: 37.24N, -115.81W'</span> </pre></div> </div> <p>Accessing arguments’ attributes:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="mi">3</span><span class="o">-</span><span class="mi">5</span><span class="n">j</span> <span class="gp">>>> </span><span class="p">(</span><span class="s1">'The complex number </span><span class="si">{0}</span><span class="s1"> is formed from the real part </span><span class="si">{0.real}</span><span class="s1"> '</span> <span class="gp">... </span> <span class="s1">'and the imaginary part </span><span class="si">{0.imag}</span><span class="s1">.'</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="go">'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'</span> <span class="gp">>>> </span><span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span> <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="s1">'Point(</span><span class="si">{self.x}</span><span class="s1">, </span><span class="si">{self.y}</span><span class="s1">)'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">Point</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> <span class="go">'Point(4, 2)'</span> </pre></div> </div> <p>Accessing arguments’ items:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">coord</span> <span class="o">=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="gp">>>> </span><span class="s1">'X: </span><span class="si">{0[0]}</span><span class="s1">; Y: </span><span class="si">{0[1]}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span> <span class="go">'X: 3; Y: 5'</span> </pre></div> </div> <p>Replacing <code class="docutils literal notranslate"><span class="pre">%s</span></code> and <code class="docutils literal notranslate"><span class="pre">%r</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s2">"repr() shows quotes: </span><span class="si">{!r}</span><span class="s2">; str() doesn't: </span><span class="si">{!s}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'test1'</span><span class="p">,</span> <span class="s1">'test2'</span><span class="p">)</span> <span class="go">"repr() shows quotes: 'test1'; str() doesn't: test2"</span> </pre></div> </div> <p>Aligning the text and specifying a width:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="si">{:<30}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'left aligned'</span><span class="p">)</span> <span class="go">'left aligned '</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{:>30}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'right aligned'</span><span class="p">)</span> <span class="go">' right aligned'</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{:^30}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'centered'</span><span class="p">)</span> <span class="go">' centered '</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{:*^30}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'centered'</span><span class="p">)</span> <span class="c1"># use '*' as a fill char</span> <span class="go">'***********centered***********'</span> </pre></div> </div> <p>Replacing <code class="docutils literal notranslate"><span class="pre">%+f</span></code>, <code class="docutils literal notranslate"><span class="pre">%-f</span></code>, and <code class="docutils literal notranslate"><span class="pre">%</span> <span class="pre">f</span></code> and specifying a sign:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="si">{:+f}</span><span class="s1">; </span><span class="si">{:+f}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.14</span><span class="p">)</span> <span class="c1"># show it always</span> <span class="go">'+3.140000; -3.140000'</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{: f}</span><span class="s1">; </span><span class="si">{: f}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.14</span><span class="p">)</span> <span class="c1"># show a space for positive numbers</span> <span class="go">' 3.140000; -3.140000'</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{:-f}</span><span class="s1">; </span><span class="si">{:-f}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.14</span><span class="p">)</span> <span class="c1"># show only the minus -- same as '{:f}; {:f}'</span> <span class="go">'3.140000; -3.140000'</span> </pre></div> </div> <p>Replacing <code class="docutils literal notranslate"><span class="pre">%x</span></code> and <code class="docutils literal notranslate"><span class="pre">%o</span></code> and converting the value to different bases:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># format also supports binary numbers</span> <span class="gp">>>> </span><span class="s2">"int: </span><span class="si">{0:d}</span><span class="s2">; hex: </span><span class="si">{0:x}</span><span class="s2">; oct: </span><span class="si">{0:o}</span><span class="s2">; bin: </span><span class="si">{0:b}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="go">'int: 42; hex: 2a; oct: 52; bin: 101010'</span> <span class="gp">>>> </span><span class="c1"># with 0x, 0o, or 0b as prefix:</span> <span class="gp">>>> </span><span class="s2">"int: </span><span class="si">{0:d}</span><span class="s2">; hex: </span><span class="si">{0:#x}</span><span class="s2">; oct: </span><span class="si">{0:#o}</span><span class="s2">; bin: </span><span class="si">{0:#b}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="go">'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'</span> </pre></div> </div> <p>Using the comma as a thousands separator:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="si">{:,}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">1234567890</span><span class="p">)</span> <span class="go">'1,234,567,890'</span> </pre></div> </div> <p>Expressing a percentage:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">points</span> <span class="o">=</span> <span class="mf">19.5</span> <span class="gp">>>> </span><span class="n">total</span> <span class="o">=</span> <span class="mi">22</span> <span class="gp">>>> </span><span class="s1">'Correct answers: </span><span class="si">{:.2%}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">points</span><span class="o">/</span><span class="n">total</span><span class="p">)</span> <span class="go">'Correct answers: 88.64%'</span> </pre></div> </div> <p>Using type-specific formatting:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">datetime</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2010</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">58</span><span class="p">)</span> <span class="gp">>>> </span><span class="s1">'{:%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="go">'2010-07-04 12:15:58'</span> </pre></div> </div> <p>Nesting arguments and more complex examples:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">align</span><span class="p">,</span> <span class="n">text</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="s1">'<^>'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'left'</span><span class="p">,</span> <span class="s1">'center'</span><span class="p">,</span> <span class="s1">'right'</span><span class="p">]):</span> <span class="gp">... </span> <span class="s1">'{0:</span><span class="si">{fill}{align}</span><span class="s1">16}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="n">align</span><span class="p">,</span> <span class="n">align</span><span class="o">=</span><span class="n">align</span><span class="p">)</span> <span class="gp">...</span> <span class="go">'left<<<<<<<<<<<<'</span> <span class="go">'^^^^^center^^^^^'</span> <span class="go">'>>>>>>>>>>>right'</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="n">octets</span> <span class="o">=</span> <span class="p">[</span><span class="mi">192</span><span class="p">,</span> <span class="mi">168</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="gp">>>> </span><span class="s1">'</span><span class="si">{:02X}{:02X}{:02X}{:02X}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="n">octets</span><span class="p">)</span> <span class="go">'C0A80001'</span> <span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span> <span class="go">3232235521</span> <span class="go">>>></span> <span class="gp">>>> </span><span class="n">width</span> <span class="o">=</span> <span class="mi">5</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">12</span><span class="p">):</span> <span class="gp">... </span> <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="s1">'dXob'</span><span class="p">:</span> <span class="gp">... </span> <span class="nb">print</span> <span class="s1">'{0:</span><span class="si">{width}{base}</span><span class="s1">}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="n">base</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="n">width</span><span class="p">),</span> <span class="gp">... </span> <span class="nb">print</span> <span class="gp">...</span> <span class="go"> 5 5 5 101</span> <span class="go"> 6 6 6 110</span> <span class="go"> 7 7 7 111</span> <span class="go"> 8 8 10 1000</span> <span class="go"> 9 9 11 1001</span> <span class="go"> 10 A 12 1010</span> <span class="go"> 11 B 13 1011</span> </pre></div> </div> </div> </div> <div class="section" id="template-strings"> <h2>7.1.4. Template strings<a class="headerlink" href="#template-strings" title="Permalink to this headline">¶</a></h2> <div class="versionadded"> <p><span class="versionmodified">New in version 2.4.</span></p> </div> <p>Templates provide simpler string substitutions as described in <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0292"><strong>PEP 292</strong></a>. Instead of the normal <code class="docutils literal notranslate"><span class="pre">%</span></code>-based substitutions, Templates support <code class="docutils literal notranslate"><span class="pre">$</span></code>-based substitutions, using the following rules:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">$$</span></code> is an escape; it is replaced with a single <code class="docutils literal notranslate"><span class="pre">$</span></code>.</li> <li><code class="docutils literal notranslate"><span class="pre">$identifier</span></code> names a substitution placeholder matching a mapping key of <code class="docutils literal notranslate"><span class="pre">"identifier"</span></code>. By default, <code class="docutils literal notranslate"><span class="pre">"identifier"</span></code> must spell a Python identifier. The first non-identifier character after the <code class="docutils literal notranslate"><span class="pre">$</span></code> character terminates this placeholder specification.</li> <li><code class="docutils literal notranslate"><span class="pre">${identifier}</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">$identifier</span></code>. It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as <code class="docutils literal notranslate"><span class="pre">"${noun}ification"</span></code>.</li> </ul> <p>Any other appearance of <code class="docutils literal notranslate"><span class="pre">$</span></code> in the string will result in a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> being raised.</p> <p>The <a class="reference internal" href="#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module provides a <a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> class that implements these rules. The methods of <a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> are:</p> <dl class="class"> <dt id="string.Template"> <em class="property">class </em><code class="descclassname">string.</code><code class="descname">Template</code><span class="sig-paren">(</span><em>template</em><span class="sig-paren">)</span><a class="headerlink" href="#string.Template" title="Permalink to this definition">¶</a></dt> <dd><p>The constructor takes a single argument which is the template string.</p> <dl class="method"> <dt id="string.Template.substitute"> <code class="descname">substitute</code><span class="sig-paren">(</span><em>mapping</em><span class="optional">[</span>, <em>**kws</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.Template.substitute" title="Permalink to this definition">¶</a></dt> <dd><p>Performs the template substitution, returning a new string. <em>mapping</em> is any dictionary-like object with keys that match the placeholders in the template. Alternatively, you can provide keyword arguments, where the keywords are the placeholders. When both <em>mapping</em> and <em>kws</em> are given and there are duplicates, the placeholders from <em>kws</em> take precedence.</p> </dd></dl> <dl class="method"> <dt id="string.Template.safe_substitute"> <code class="descname">safe_substitute</code><span class="sig-paren">(</span><em>mapping</em><span class="optional">[</span>, <em>**kws</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.Template.safe_substitute" title="Permalink to this definition">¶</a></dt> <dd><p>Like <a class="reference internal" href="#string.Template.substitute" title="string.Template.substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code></a>, except that if placeholders are missing from <em>mapping</em> and <em>kws</em>, instead of raising a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception, the original placeholder will appear in the resulting string intact. Also, unlike with <a class="reference internal" href="#string.Template.substitute" title="string.Template.substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code></a>, any other appearances of the <code class="docutils literal notranslate"><span class="pre">$</span></code> will simply return <code class="docutils literal notranslate"><span class="pre">$</span></code> instead of raising <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p> <p>While other exceptions may still occur, this method is called “safe” because it always tries to return a usable string instead of raising an exception. In another sense, <a class="reference internal" href="#string.Template.safe_substitute" title="string.Template.safe_substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">safe_substitute()</span></code></a> may be anything other than safe, since it will silently ignore malformed templates containing dangling delimiters, unmatched braces, or placeholders that are not valid Python identifiers.</p> </dd></dl> <p><a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> instances also provide one public data attribute:</p> <dl class="attribute"> <dt id="string.Template.template"> <code class="descname">template</code><a class="headerlink" href="#string.Template.template" title="Permalink to this definition">¶</a></dt> <dd><p>This is the object passed to the constructor’s <em>template</em> argument. In general, you shouldn’t change it, but read-only access is not enforced.</p> </dd></dl> </dd></dl> <p>Here is an example of how to use a Template:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">string</span> <span class="k">import</span> <span class="n">Template</span> <span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s1">'$who likes $what'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">who</span><span class="o">=</span><span class="s1">'tim'</span><span class="p">,</span> <span class="n">what</span><span class="o">=</span><span class="s1">'kung pao'</span><span class="p">)</span> <span class="go">'tim likes kung pao'</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">who</span><span class="o">=</span><span class="s1">'tim'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">Template</span><span class="p">(</span><span class="s1">'Give $who $100'</span><span class="p">)</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> <span class="c">...</span> <span class="gr">ValueError</span>: <span class="n">Invalid placeholder in string: line 1, col 11</span> <span class="gp">>>> </span><span class="n">Template</span><span class="p">(</span><span class="s1">'$who likes $what'</span><span class="p">)</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> <span class="c">...</span> <span class="gr">KeyError</span>: <span class="n">'what'</span> <span class="gp">>>> </span><span class="n">Template</span><span class="p">(</span><span class="s1">'$who likes $what'</span><span class="p">)</span><span class="o">.</span><span class="n">safe_substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="go">'tim likes $what'</span> </pre></div> </div> <p>Advanced usage: you can derive subclasses of <a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> to customize the placeholder syntax, delimiter character, or the entire regular expression used to parse template strings. To do this, you can override these class attributes:</p> <ul class="simple"> <li><em>delimiter</em> – This is the literal string describing a placeholder introducing delimiter. The default value is <code class="docutils literal notranslate"><span class="pre">$</span></code>. Note that this should <em>not</em> be a regular expression, as the implementation will call <a class="reference internal" href="re.html#re.escape" title="re.escape"><code class="xref py py-meth docutils literal notranslate"><span class="pre">re.escape()</span></code></a> on this string as needed.</li> <li><em>idpattern</em> – This is the regular expression describing the pattern for non-braced placeholders (the braces will be added automatically as appropriate). The default value is the regular expression <code class="docutils literal notranslate"><span class="pre">[_a-z][_a-z0-9]*</span></code>.</li> </ul> <p>Alternatively, you can provide the entire regular expression pattern by overriding the class attribute <em>pattern</em>. If you do this, the value must be a regular expression object with four named capturing groups. The capturing groups correspond to the rules given above, along with the invalid placeholder rule:</p> <ul class="simple"> <li><em>escaped</em> – This group matches the escape sequence, e.g. <code class="docutils literal notranslate"><span class="pre">$$</span></code>, in the default pattern.</li> <li><em>named</em> – This group matches the unbraced placeholder name; it should not include the delimiter in capturing group.</li> <li><em>braced</em> – This group matches the brace enclosed placeholder name; it should not include either the delimiter or braces in the capturing group.</li> <li><em>invalid</em> – This group matches any other delimiter pattern (usually a single delimiter), and it should appear last in the regular expression.</li> </ul> </div> <div class="section" id="string-functions"> <h2>7.1.5. String functions<a class="headerlink" href="#string-functions" title="Permalink to this headline">¶</a></h2> <p>The following functions are available to operate on string and Unicode objects. They are not available as string methods.</p> <dl class="function"> <dt id="string.capwords"> <code class="descclassname">string.</code><code class="descname">capwords</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.capwords" title="Permalink to this definition">¶</a></dt> <dd><p>Split the argument into words using <a class="reference internal" href="stdtypes.html#str.split" title="str.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.split()</span></code></a>, capitalize each word using <a class="reference internal" href="stdtypes.html#str.capitalize" title="str.capitalize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.capitalize()</span></code></a>, and join the capitalized words using <a class="reference internal" href="stdtypes.html#str.join" title="str.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.join()</span></code></a>. If the optional second argument <em>sep</em> is absent or <code class="docutils literal notranslate"><span class="pre">None</span></code>, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise <em>sep</em> is used to split and join the words.</p> </dd></dl> <dl class="function"> <dt id="string.maketrans"> <code class="descclassname">string.</code><code class="descname">maketrans</code><span class="sig-paren">(</span><em>from</em>, <em>to</em><span class="sig-paren">)</span><a class="headerlink" href="#string.maketrans" title="Permalink to this definition">¶</a></dt> <dd><p>Return a translation table suitable for passing to <a class="reference internal" href="#string.translate" title="string.translate"><code class="xref py py-func docutils literal notranslate"><span class="pre">translate()</span></code></a>, that will map each character in <em>from</em> into the character at the same position in <em>to</em>; <em>from</em> and <em>to</em> must have the same length.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Don’t use strings derived from <a class="reference internal" href="#string.lowercase" title="string.lowercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">lowercase</span></code></a> and <a class="reference internal" href="#string.uppercase" title="string.uppercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">uppercase</span></code></a> as arguments; in some locales, these don’t have the same length. For case conversions, always use <a class="reference internal" href="stdtypes.html#str.lower" title="str.lower"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.lower()</span></code></a> and <a class="reference internal" href="stdtypes.html#str.upper" title="str.upper"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.upper()</span></code></a>.</p> </div> </dd></dl> </div> <div class="section" id="deprecated-string-functions"> <h2>7.1.6. Deprecated string functions<a class="headerlink" href="#deprecated-string-functions" title="Permalink to this headline">¶</a></h2> <p>The following list of functions are also defined as methods of string and Unicode objects; see section <a class="reference internal" href="stdtypes.html#string-methods"><span class="std std-ref">String Methods</span></a> for more information on those. You should consider these functions as deprecated, although they will not be removed until Python 3. The functions defined in this module are:</p> <dl class="function"> <dt id="string.atof"> <code class="descclassname">string.</code><code class="descname">atof</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#string.atof" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.0: </span>Use the <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a> built-in function.</p> </div> <p id="index-5">Convert a string to a floating point number. The string must have the standard syntax for a floating point literal in Python, optionally preceded by a sign (<code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code>). Note that this behaves identical to the built-in function <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a> when passed a string.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last" id="index-6">When passing in a string, values for NaN and Infinity may be returned, depending on the underlying C library. The specific set of strings accepted which cause these values to be returned depends entirely on the C library and is known to vary.</p> </div> </dd></dl> <dl class="function"> <dt id="string.atoi"> <code class="descclassname">string.</code><code class="descname">atoi</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>base</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.atoi" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.0: </span>Use the <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> built-in function.</p> </div> <p id="index-7">Convert string <em>s</em> to an integer in the given <em>base</em>. The string must consist of one or more digits, optionally preceded by a sign (<code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code>). The <em>base</em> defaults to 10. If it is 0, a default base is chosen depending on the leading characters of the string (after stripping the sign): <code class="docutils literal notranslate"><span class="pre">0x</span></code> or <code class="docutils literal notranslate"><span class="pre">0X</span></code> means 16, <code class="docutils literal notranslate"><span class="pre">0</span></code> means 8, anything else means 10. If <em>base</em> is 16, a leading <code class="docutils literal notranslate"><span class="pre">0x</span></code> or <code class="docutils literal notranslate"><span class="pre">0X</span></code> is always accepted, though not required. This behaves identically to the built-in function <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> when passed a string. (Also note: for a more flexible interpretation of numeric literals, use the built-in function <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>.)</p> </dd></dl> <dl class="function"> <dt id="string.atol"> <code class="descclassname">string.</code><code class="descname">atol</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>base</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.atol" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 2.0: </span>Use the <a class="reference internal" href="functions.html#long" title="long"><code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code></a> built-in function.</p> </div> <p id="index-8">Convert string <em>s</em> to a long integer in the given <em>base</em>. The string must consist of one or more digits, optionally preceded by a sign (<code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code>). The <em>base</em> argument has the same meaning as for <a class="reference internal" href="#string.atoi" title="string.atoi"><code class="xref py py-func docutils literal notranslate"><span class="pre">atoi()</span></code></a>. A trailing <code class="docutils literal notranslate"><span class="pre">l</span></code> or <code class="docutils literal notranslate"><span class="pre">L</span></code> is not allowed, except if the base is 0. Note that when invoked without <em>base</em> or with <em>base</em> set to 10, this behaves identical to the built-in function <a class="reference internal" href="functions.html#long" title="long"><code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code></a> when passed a string.</p> </dd></dl> <dl class="function"> <dt id="string.capitalize"> <code class="descclassname">string.</code><code class="descname">capitalize</code><span class="sig-paren">(</span><em>word</em><span class="sig-paren">)</span><a class="headerlink" href="#string.capitalize" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of <em>word</em> with only its first character capitalized.</p> </dd></dl> <dl class="function"> <dt id="string.expandtabs"> <code class="descclassname">string.</code><code class="descname">expandtabs</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>tabsize</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.expandtabs" title="Permalink to this definition">¶</a></dt> <dd><p>Expand tabs in a string replacing them by one or more spaces, depending on the current column and the given tab size. The column number is reset to zero after each newline occurring in the string. This doesn’t understand other non-printing characters or escape sequences. The tab size defaults to 8.</p> </dd></dl> <dl class="function"> <dt id="string.find"> <code class="descclassname">string.</code><code class="descname">find</code><span class="sig-paren">(</span><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.find" title="Permalink to this definition">¶</a></dt> <dd><p>Return the lowest index in <em>s</em> where the substring <em>sub</em> is found such that <em>sub</em> is wholly contained in <code class="docutils literal notranslate"><span class="pre">s[start:end]</span></code>. Return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure. Defaults for <em>start</em> and <em>end</em> and interpretation of negative values is the same as for slices.</p> </dd></dl> <dl class="function"> <dt id="string.rfind"> <code class="descclassname">string.</code><code class="descname">rfind</code><span class="sig-paren">(</span><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.rfind" title="Permalink to this definition">¶</a></dt> <dd><p>Like <a class="reference internal" href="#string.find" title="string.find"><code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code></a> but find the highest index.</p> </dd></dl> <dl class="function"> <dt id="string.index"> <code class="descclassname">string.</code><code class="descname">index</code><span class="sig-paren">(</span><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.index" title="Permalink to this definition">¶</a></dt> <dd><p>Like <a class="reference internal" href="#string.find" title="string.find"><code class="xref py py-func docutils literal notranslate"><span class="pre">find()</span></code></a> but raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> when the substring is not found.</p> </dd></dl> <dl class="function"> <dt id="string.rindex"> <code class="descclassname">string.</code><code class="descname">rindex</code><span class="sig-paren">(</span><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.rindex" title="Permalink to this definition">¶</a></dt> <dd><p>Like <a class="reference internal" href="#string.rfind" title="string.rfind"><code class="xref py py-func docutils literal notranslate"><span class="pre">rfind()</span></code></a> but raise <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> when the substring is not found.</p> </dd></dl> <dl class="function"> <dt id="string.count"> <code class="descclassname">string.</code><code class="descname">count</code><span class="sig-paren">(</span><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.count" title="Permalink to this definition">¶</a></dt> <dd><p>Return the number of (non-overlapping) occurrences of substring <em>sub</em> in string <code class="docutils literal notranslate"><span class="pre">s[start:end]</span></code>. Defaults for <em>start</em> and <em>end</em> and interpretation of negative values are the same as for slices.</p> </dd></dl> <dl class="function"> <dt id="string.lower"> <code class="descclassname">string.</code><code class="descname">lower</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#string.lower" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of <em>s</em>, but with upper case letters converted to lower case.</p> </dd></dl> <dl class="function"> <dt id="string.split"> <code class="descclassname">string.</code><code class="descname">split</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">[</span>, <em>maxsplit</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.split" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of the words of the string <em>s</em>. If the optional second argument <em>sep</em> is absent or <code class="docutils literal notranslate"><span class="pre">None</span></code>, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument <em>sep</em> is present and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. If <em>maxsplit</em> is given, at most <em>maxsplit</em> number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most <code class="docutils literal notranslate"><span class="pre">maxsplit+1</span></code> elements). If <em>maxsplit</em> is not specified or <code class="docutils literal notranslate"><span class="pre">-1</span></code>, then there is no limit on the number of splits (all possible splits are made).</p> <p>The behavior of split on an empty string depends on the value of <em>sep</em>. If <em>sep</em> is not specified, or specified as <code class="docutils literal notranslate"><span class="pre">None</span></code>, the result will be an empty list. If <em>sep</em> is specified as any string, the result will be a list containing one element which is an empty string.</p> </dd></dl> <dl class="function"> <dt id="string.rsplit"> <code class="descclassname">string.</code><code class="descname">rsplit</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">[</span>, <em>maxsplit</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.rsplit" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of the words of the string <em>s</em>, scanning <em>s</em> from the end. To all intents and purposes, the resulting list of words is the same as returned by <a class="reference internal" href="#string.split" title="string.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a>, except when the optional third argument <em>maxsplit</em> is explicitly specified and nonzero. If <em>maxsplit</em> is given, at most <em>maxsplit</em> number of splits – the <em>rightmost</em> ones – occur, and the remainder of the string is returned as the first element of the list (thus, the list will have at most <code class="docutils literal notranslate"><span class="pre">maxsplit+1</span></code> elements).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.4.</span></p> </div> </dd></dl> <dl class="function"> <dt id="string.splitfields"> <code class="descclassname">string.</code><code class="descname">splitfields</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">[</span>, <em>maxsplit</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.splitfields" title="Permalink to this definition">¶</a></dt> <dd><p>This function behaves identically to <a class="reference internal" href="#string.split" title="string.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a>. (In the past, <a class="reference internal" href="#string.split" title="string.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a> was only used with one argument, while <a class="reference internal" href="#string.splitfields" title="string.splitfields"><code class="xref py py-func docutils literal notranslate"><span class="pre">splitfields()</span></code></a> was only used with two arguments.)</p> </dd></dl> <dl class="function"> <dt id="string.join"> <code class="descclassname">string.</code><code class="descname">join</code><span class="sig-paren">(</span><em>words</em><span class="optional">[</span>, <em>sep</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.join" title="Permalink to this definition">¶</a></dt> <dd><p>Concatenate a list or tuple of words with intervening occurrences of <em>sep</em>. The default value for <em>sep</em> is a single space character. It is always true that <code class="docutils literal notranslate"><span class="pre">string.join(string.split(s,</span> <span class="pre">sep),</span> <span class="pre">sep)</span></code> equals <em>s</em>.</p> </dd></dl> <dl class="function"> <dt id="string.joinfields"> <code class="descclassname">string.</code><code class="descname">joinfields</code><span class="sig-paren">(</span><em>words</em><span class="optional">[</span>, <em>sep</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.joinfields" title="Permalink to this definition">¶</a></dt> <dd><p>This function behaves identically to <a class="reference internal" href="#string.join" title="string.join"><code class="xref py py-func docutils literal notranslate"><span class="pre">join()</span></code></a>. (In the past, <a class="reference internal" href="#string.join" title="string.join"><code class="xref py py-func docutils literal notranslate"><span class="pre">join()</span></code></a> was only used with one argument, while <a class="reference internal" href="#string.joinfields" title="string.joinfields"><code class="xref py py-func docutils literal notranslate"><span class="pre">joinfields()</span></code></a> was only used with two arguments.) Note that there is no <a class="reference internal" href="#string.joinfields" title="string.joinfields"><code class="xref py py-meth docutils literal notranslate"><span class="pre">joinfields()</span></code></a> method on string objects; use the <a class="reference internal" href="#string.join" title="string.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> method instead.</p> </dd></dl> <dl class="function"> <dt id="string.lstrip"> <code class="descclassname">string.</code><code class="descname">lstrip</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>chars</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.lstrip" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of the string with leading characters removed. If <em>chars</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, whitespace characters are removed. If given and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>chars</em> must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2.3: </span>The <em>chars</em> parameter was added. The <em>chars</em> parameter cannot be passed in earlier 2.2 versions.</p> </div> </dd></dl> <dl class="function"> <dt id="string.rstrip"> <code class="descclassname">string.</code><code class="descname">rstrip</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>chars</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.rstrip" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of the string with trailing characters removed. If <em>chars</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, whitespace characters are removed. If given and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>chars</em> must be a string; the characters in the string will be stripped from the end of the string this method is called on.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2.3: </span>The <em>chars</em> parameter was added. The <em>chars</em> parameter cannot be passed in earlier 2.2 versions.</p> </div> </dd></dl> <dl class="function"> <dt id="string.strip"> <code class="descclassname">string.</code><code class="descname">strip</code><span class="sig-paren">(</span><em>s</em><span class="optional">[</span>, <em>chars</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.strip" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of the string with leading and trailing characters removed. If <em>chars</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, whitespace characters are removed. If given and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>chars</em> must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.2.3: </span>The <em>chars</em> parameter was added. The <em>chars</em> parameter cannot be passed in earlier 2.2 versions.</p> </div> </dd></dl> <dl class="function"> <dt id="string.swapcase"> <code class="descclassname">string.</code><code class="descname">swapcase</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#string.swapcase" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of <em>s</em>, but with lower case letters converted to upper case and vice versa.</p> </dd></dl> <dl class="function"> <dt id="string.translate"> <code class="descclassname">string.</code><code class="descname">translate</code><span class="sig-paren">(</span><em>s</em>, <em>table</em><span class="optional">[</span>, <em>deletechars</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.translate" title="Permalink to this definition">¶</a></dt> <dd><p>Delete all characters from <em>s</em> that are in <em>deletechars</em> (if present), and then translate the characters using <em>table</em>, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If <em>table</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then only the character deletion step is performed.</p> </dd></dl> <dl class="function"> <dt id="string.upper"> <code class="descclassname">string.</code><code class="descname">upper</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#string.upper" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of <em>s</em>, but with lower case letters converted to upper case.</p> </dd></dl> <dl class="function"> <dt id="string.ljust"> <code class="descclassname">string.</code><code class="descname">ljust</code><span class="sig-paren">(</span><em>s</em>, <em>width</em><span class="optional">[</span>, <em>fillchar</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.ljust" title="Permalink to this definition">¶</a></dt> <dt id="string.rjust"> <code class="descclassname">string.</code><code class="descname">rjust</code><span class="sig-paren">(</span><em>s</em>, <em>width</em><span class="optional">[</span>, <em>fillchar</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.rjust" title="Permalink to this definition">¶</a></dt> <dt id="string.center"> <code class="descclassname">string.</code><code class="descname">center</code><span class="sig-paren">(</span><em>s</em>, <em>width</em><span class="optional">[</span>, <em>fillchar</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.center" title="Permalink to this definition">¶</a></dt> <dd><p>These functions respectively left-justify, right-justify and center a string in a field of given width. They return a string that is at least <em>width</em> characters wide, created by padding the string <em>s</em> with the character <em>fillchar</em> (default is a space) until the given width on the right, left or both sides. The string is never truncated.</p> </dd></dl> <dl class="function"> <dt id="string.zfill"> <code class="descclassname">string.</code><code class="descname">zfill</code><span class="sig-paren">(</span><em>s</em>, <em>width</em><span class="sig-paren">)</span><a class="headerlink" href="#string.zfill" title="Permalink to this definition">¶</a></dt> <dd><p>Pad a numeric string <em>s</em> on the left with zero digits until the given <em>width</em> is reached. Strings starting with a sign are handled correctly.</p> </dd></dl> <dl class="function"> <dt id="string.replace"> <code class="descclassname">string.</code><code class="descname">replace</code><span class="sig-paren">(</span><em>s</em>, <em>old</em>, <em>new</em><span class="optional">[</span>, <em>maxreplace</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#string.replace" title="Permalink to this definition">¶</a></dt> <dd><p>Return a copy of string <em>s</em> with all occurrences of substring <em>old</em> replaced by <em>new</em>. If the optional argument <em>maxreplace</em> is given, the first <em>maxreplace</em> occurrences are replaced.</p> </dd></dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">7.1. <code class="docutils literal notranslate"><span class="pre">string</span></code> — Common string operations</a><ul> <li><a class="reference internal" href="#string-constants">7.1.1. String constants</a></li> <li><a class="reference internal" href="#custom-string-formatting">7.1.2. Custom String Formatting</a></li> <li><a class="reference internal" href="#format-string-syntax">7.1.3. Format String Syntax</a><ul> <li><a class="reference internal" href="#format-specification-mini-language">7.1.3.1. Format Specification Mini-Language</a></li> <li><a class="reference internal" href="#format-examples">7.1.3.2. Format examples</a></li> </ul> </li> <li><a class="reference internal" href="#template-strings">7.1.4. Template strings</a></li> <li><a class="reference internal" href="#string-functions">7.1.5. String functions</a></li> <li><a class="reference internal" href="#deprecated-string-functions">7.1.6. Deprecated string functions</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="strings.html" title="previous chapter">7. String Services</a></p> <h4>Next topic</h4> <p class="topless"><a href="re.html" title="next chapter">7.2. <code class="docutils literal notranslate"><span class="pre">re</span></code> — Regular expression operations</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/library/string.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="re.html" title="7.2. re — Regular expression operations" >next</a> |</li> <li class="right" > <a href="strings.html" title="7. String Services" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.16 documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="strings.html" >7. String Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Mar 27, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>
Save
cmd:
run