parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="locale — Internationalization services" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/locale.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/locale.py The locale module opens access to the POSIX locale database and functionality. The POSIX locale mechanism allows programmers to deal with certain cultural issues in an ap..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_locale_339cdb28.png" />15<meta property="og:image:alt" content="Source code: Lib/locale.py The locale module opens access to the POSIX locale database and functionality. The POSIX locale mechanism allows programmers to deal with certain cultural issues in an ap..." />16<meta name="description" content="Source code: Lib/locale.py The locale module opens access to the POSIX locale database and functionality. The POSIX locale mechanism allows programmers to deal with certain cultural issues in an ap..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>locale — Internationalization services — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="Graphical user interfaces with Tk" href="tk.html" />43 <link rel="prev" title="gettext — Multilingual internationalization services" href="gettext.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/locale.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a><ul>108<li><a class="reference internal" href="#background-details-hints-tips-and-caveats">Background, details, hints, tips and caveats</a></li>109<li><a class="reference internal" href="#locale-names">Locale names</a></li>110<li><a class="reference internal" href="#for-extension-writers-and-programs-that-embed-python">For extension writers and programs that embed Python</a></li>111<li><a class="reference internal" href="#access-to-message-catalogs">Access to message catalogs</a></li>112</ul>113</li>114</ul>115 116 </div>117 <div>118 <h4>Previous topic</h4>119 <p class="topless"><a href="gettext.html"120 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a></p>121 </div>122 <div>123 <h4>Next topic</h4>124 <p class="topless"><a href="tk.html"125 title="next chapter">Graphical user interfaces with Tk</a></p>126 </div>127 <script>128 document.addEventListener('DOMContentLoaded', () => {129 const title = document.querySelector('meta[property="og:title"]').content;130 const elements = document.querySelectorAll('.improvepage');131 const pageurl = window.location.href.split('?')[0];132 elements.forEach(element => {133 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));134 url.searchParams.set('pagetitle', title);135 url.searchParams.set('pageurl', pageurl);136 url.searchParams.set('pagesource', "library/locale.rst");137 element.href = url.toString();138 });139 });140 </script>141 <div role="note" aria-label="source link">142 <h3>This page</h3>143 <ul class="this-page-menu">144 <li><a href="../bugs.html">Report a bug</a></li>145 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>146 <li>147 <a href="https://github.com/python/cpython/blob/main/Doc/library/locale.rst?plain=1"148 rel="nofollow">Show source149 </a>150 </li>151 152 </ul>153 </div>154 </nav>155 </div>156</div>157 158 159 <div class="related" role="navigation" aria-label="Related">160 <h3>Navigation</h3>161 <ul>162 <li class="right" style="margin-right: 10px">163 <a href="../genindex.html" title="General Index"164 accesskey="I">index</a></li>165 <li class="right" >166 <a href="../py-modindex.html" title="Python Module Index"167 >modules</a> |</li>168 <li class="right" >169 <a href="tk.html" title="Graphical user interfaces with Tk"170 accesskey="N">next</a> |</li>171 <li class="right" >172 <a href="gettext.html" title="gettext — Multilingual internationalization services"173 accesskey="P">previous</a> |</li>174 175 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>176 <li><a href="https://www.python.org/">Python</a> »</li>177 <li class="switchers">178 <div class="language_switcher_placeholder"></div>179 <div class="version_switcher_placeholder"></div>180 </li>181 <li>182 183 </li>184 <li id="cpython-language-and-version">185 <a href="../index.html">3.15.0a6 Documentation</a> »186 </li>187 188 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>189 <li class="nav-item nav-item-2"><a href="i18n.html" accesskey="U">Internationalization</a> »</li>190 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a></li>191 <li class="right">192 193 194 <div class="inline-search" role="search">195 <form class="inline-search" action="../search.html" method="get">196 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">197 <input type="submit" value="Go">198 </form>199 </div>200 |201 </li>202 <li class="right">203<label class="theme-selector-label">204 Theme205 <select class="theme-selector" oninput="activateTheme(this.value)">206 <option value="auto" selected>Auto</option>207 <option value="light">Light</option>208 <option value="dark">Dark</option>209 </select>210</label> |</li>211 212 </ul>213 </div> 214 215 <div class="document">216 <div class="documentwrapper">217 <div class="bodywrapper">218 <div class="body" role="main">219 220 <section id="module-locale">221<span id="locale-internationalization-services"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services<a class="headerlink" href="#module-locale" title="Link to this heading">¶</a></h1>222<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/locale.py">Lib/locale.py</a></p>223<hr class="docutils" />224<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> module opens access to the POSIX locale database and225functionality. The POSIX locale mechanism allows programmers to deal with226certain cultural issues in an application, without requiring the programmer to227know all the specifics of each country where the software is executed.</p>228<p id="index-0">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> module is implemented on top of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_locale</span></code> module,229which in turn uses an ANSI C locale implementation if available.</p>230<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> module defines the following exception and functions:</p>231<dl class="py exception">232<dt class="sig sig-object py" id="locale.Error">233<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#locale.Error" title="Link to this definition">¶</a></dt>234<dd><p>Exception raised when the locale passed to <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> is not235recognized.</p>236</dd></dl>237 238<dl class="py function">239<dt class="sig sig-object py" id="locale.setlocale">240<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">setlocale</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">category</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locale</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.setlocale" title="Link to this definition">¶</a></dt>241<dd><p>If <em>locale</em> is given and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code> modifies the locale242setting for the <em>category</em>. The available categories are listed in the data243description below. <em>locale</em> may be a <a class="reference internal" href="#locale-name"><span class="std std-ref">string</span></a>, or a pair,244language code and encoding. An empty string specifies the user’s245default settings. If the modification of the locale fails, the exception246<a class="reference internal" href="#locale.Error" title="locale.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a> is raised. If successful, the new locale setting is returned.</p>247<p>If <em>locale</em> is a pair, it is converted to a locale name using248the locale aliasing engine.249The language code has the same format as a <a class="reference internal" href="#locale-name"><span class="std std-ref">locale name</span></a>,250but without encoding.251The language code and encoding can be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>252<p>If <em>locale</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, the current setting for <em>category</em> is253returned.</p>254<p>Example:</p>255<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">locale</span>256<span class="gp">>>> </span><span class="n">loc</span> <span class="o">=</span> <span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">)</span> <span class="c1"># get current locale</span>257<span class="go"># use German locale; name and availability varies with platform</span>258<span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">'de_DE.UTF-8'</span><span class="p">)</span>259<span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">strcoll</span><span class="p">(</span><span class="s1">'f</span><span class="se">\xe4</span><span class="s1">n'</span><span class="p">,</span> <span class="s1">'foo'</span><span class="p">)</span> <span class="c1"># compare a string containing an umlaut</span>260<span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span> <span class="c1"># use user's preferred locale</span>261<span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">)</span> <span class="c1"># use default (C) locale</span>262<span class="gp">>>> </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="n">loc</span><span class="p">)</span> <span class="c1"># restore saved locale</span>263</pre></div>264</div>265<p><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code> is not thread-safe on most systems. Applications typically266start with a call of:</p>267<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">locale</span>268<span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>269</pre></div>270</div>271<p>This sets the locale for all categories to the user’s default setting (typically272specified in the <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code> environment variable). If the locale is not273changed thereafter, using multithreading should not cause problems.</p>274<div class="versionchanged">275<p><span class="versionmodified changed">Changed in version 3.15: </span>Support language codes with <code class="docutils literal notranslate"><span class="pre">@</span></code>-modifiers.</p>276</div>277</dd></dl>278 279<dl class="py function">280<dt class="sig sig-object py" id="locale.localeconv">281<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">localeconv</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#locale.localeconv" title="Link to this definition">¶</a></dt>282<dd><p>Returns the database of the local conventions as a dictionary. This dictionary283has the following strings as keys:</p>284<table class="docutils align-default">285<thead>286<tr class="row-odd"><th class="head"><p>Category</p></th>287<th class="head"><p>Key</p></th>288<th class="head"><p>Meaning</p></th>289</tr>290</thead>291<tbody>292<tr class="row-even"><td><p><a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a></p></td>293<td><p><code class="docutils literal notranslate"><span class="pre">'decimal_point'</span></code></p></td>294<td><p>Decimal point character.</p></td>295</tr>296<tr class="row-odd"><td></td>297<td><p><code class="docutils literal notranslate"><span class="pre">'grouping'</span></code></p></td>298<td><p>Sequence of numbers specifying299which relative positions the300<code class="docutils literal notranslate"><span class="pre">'thousands_sep'</span></code> is301expected. If the sequence is302terminated with303<a class="reference internal" href="#locale.CHAR_MAX" title="locale.CHAR_MAX"><code class="xref py py-const docutils literal notranslate"><span class="pre">CHAR_MAX</span></code></a>, no further304grouping is performed. If the305sequence terminates with a306<code class="docutils literal notranslate"><span class="pre">0</span></code>, the last group size is307repeatedly used.</p></td>308</tr>309<tr class="row-even"><td></td>310<td><p><code class="docutils literal notranslate"><span class="pre">'thousands_sep'</span></code></p></td>311<td><p>Character used between groups.</p></td>312</tr>313<tr class="row-odd"><td><p><a class="reference internal" href="#locale.LC_MONETARY" title="locale.LC_MONETARY"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_MONETARY</span></code></a></p></td>314<td><p><code class="docutils literal notranslate"><span class="pre">'int_curr_symbol'</span></code></p></td>315<td><p>International currency symbol.</p></td>316</tr>317<tr class="row-even"><td></td>318<td><p><code class="docutils literal notranslate"><span class="pre">'currency_symbol'</span></code></p></td>319<td><p>Local currency symbol.</p></td>320</tr>321<tr class="row-odd"><td></td>322<td><p><code class="docutils literal notranslate"><span class="pre">'p_cs_precedes/n_cs_precedes'</span></code></p></td>323<td><p>Whether the currency symbol324precedes the value (for325positive resp. negative326values).</p></td>327</tr>328<tr class="row-even"><td></td>329<td><p><code class="docutils literal notranslate"><span class="pre">'p_sep_by_space/n_sep_by_space'</span></code></p></td>330<td><p>Whether the currency symbol is331separated from the value by a332space (for positive resp.333negative values).</p></td>334</tr>335<tr class="row-odd"><td></td>336<td><p><code class="docutils literal notranslate"><span class="pre">'mon_decimal_point'</span></code></p></td>337<td><p>Decimal point used for338monetary values.</p></td>339</tr>340<tr class="row-even"><td></td>341<td><p><code class="docutils literal notranslate"><span class="pre">'frac_digits'</span></code></p></td>342<td><p>Number of fractional digits343used in local formatting of344monetary values.</p></td>345</tr>346<tr class="row-odd"><td></td>347<td><p><code class="docutils literal notranslate"><span class="pre">'int_frac_digits'</span></code></p></td>348<td><p>Number of fractional digits349used in international350formatting of monetary values.</p></td>351</tr>352<tr class="row-even"><td></td>353<td><p><code class="docutils literal notranslate"><span class="pre">'mon_thousands_sep'</span></code></p></td>354<td><p>Group separator used for355monetary values.</p></td>356</tr>357<tr class="row-odd"><td></td>358<td><p><code class="docutils literal notranslate"><span class="pre">'mon_grouping'</span></code></p></td>359<td><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">'grouping'</span></code>,360used for monetary values.</p></td>361</tr>362<tr class="row-even"><td></td>363<td><p><code class="docutils literal notranslate"><span class="pre">'positive_sign'</span></code></p></td>364<td><p>Symbol used to annotate a365positive monetary value.</p></td>366</tr>367<tr class="row-odd"><td></td>368<td><p><code class="docutils literal notranslate"><span class="pre">'negative_sign'</span></code></p></td>369<td><p>Symbol used to annotate a370negative monetary value.</p></td>371</tr>372<tr class="row-even"><td></td>373<td><p><code class="docutils literal notranslate"><span class="pre">'p_sign_posn/n_sign_posn'</span></code></p></td>374<td><p>The position of the sign (for375positive resp. negative376values), see below.</p></td>377</tr>378</tbody>379</table>380<p>All numeric values can be set to <a class="reference internal" href="#locale.CHAR_MAX" title="locale.CHAR_MAX"><code class="xref py py-const docutils literal notranslate"><span class="pre">CHAR_MAX</span></code></a> to indicate that there is no381value specified in this locale.</p>382<p>The possible values for <code class="docutils literal notranslate"><span class="pre">'p_sign_posn'</span></code> and <code class="docutils literal notranslate"><span class="pre">'n_sign_posn'</span></code> are given below.</p>383<table class="docutils align-default">384<thead>385<tr class="row-odd"><th class="head"><p>Value</p></th>386<th class="head"><p>Explanation</p></th>387</tr>388</thead>389<tbody>390<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">0</span></code></p></td>391<td><p>Currency and value are surrounded by392parentheses.</p></td>393</tr>394<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">1</span></code></p></td>395<td><p>The sign should precede the value and396currency symbol.</p></td>397</tr>398<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">2</span></code></p></td>399<td><p>The sign should follow the value and400currency symbol.</p></td>401</tr>402<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">3</span></code></p></td>403<td><p>The sign should immediately precede the404value.</p></td>405</tr>406<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">4</span></code></p></td>407<td><p>The sign should immediately follow the408value.</p></td>409</tr>410<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">CHAR_MAX</span></code></p></td>411<td><p>Nothing is specified in this locale.</p></td>412</tr>413</tbody>414</table>415<p>The function temporarily sets the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> locale to the <code class="docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code>416locale or the <code class="docutils literal notranslate"><span class="pre">LC_MONETARY</span></code> locale if locales are different and numeric or417monetary strings are non-ASCII. This temporary change affects other threads.</p>418<div class="versionchanged">419<p><span class="versionmodified changed">Changed in version 3.7: </span>The function now temporarily sets the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> locale to the420<code class="docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code> locale in some cases.</p>421</div>422</dd></dl>423 424<dl class="py function">425<dt class="sig sig-object py" id="locale.nl_langinfo">426<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">nl_langinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">option</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.nl_langinfo" title="Link to this definition">¶</a></dt>427<dd><p>Return some locale-specific information as a string. This function is not428available on all systems, and the set of possible options might also vary429across platforms. The possible argument values are numbers, for which430symbolic constants are available in the locale module.</p>431<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">nl_langinfo()</span></code> function accepts one of the following keys. Most432descriptions are taken from the corresponding description in the GNU C433library.</p>434<dl class="py data">435<dt class="sig sig-object py" id="locale.CODESET">436<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">CODESET</span></span><a class="headerlink" href="#locale.CODESET" title="Link to this definition">¶</a></dt>437<dd><p>Get a string with the name of the character encoding used in the438selected locale.</p>439</dd></dl>440 441<dl class="py data">442<dt class="sig sig-object py" id="locale.D_T_FMT">443<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">D_T_FMT</span></span><a class="headerlink" href="#locale.D_T_FMT" title="Link to this definition">¶</a></dt>444<dd><p>Get a string that can be used as a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to445represent date and time in a locale-specific way.</p>446</dd></dl>447 448<dl class="py data">449<dt class="sig sig-object py" id="locale.D_FMT">450<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">D_FMT</span></span><a class="headerlink" href="#locale.D_FMT" title="Link to this definition">¶</a></dt>451<dd><p>Get a string that can be used as a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to452represent a date in a locale-specific way.</p>453</dd></dl>454 455<dl class="py data">456<dt class="sig sig-object py" id="locale.T_FMT">457<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">T_FMT</span></span><a class="headerlink" href="#locale.T_FMT" title="Link to this definition">¶</a></dt>458<dd><p>Get a string that can be used as a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to459represent a time in a locale-specific way.</p>460</dd></dl>461 462<dl class="py data">463<dt class="sig sig-object py" id="locale.T_FMT_AMPM">464<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">T_FMT_AMPM</span></span><a class="headerlink" href="#locale.T_FMT_AMPM" title="Link to this definition">¶</a></dt>465<dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent time in the am/pm466format.</p>467</dd></dl>468 469<dl class="py data">470<dt class="sig sig-object py" id="locale.DAY_1">471<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_1</span></span><a class="headerlink" href="#locale.DAY_1" title="Link to this definition">¶</a></dt>472<dt class="sig sig-object py" id="locale.DAY_2">473<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_2</span></span><a class="headerlink" href="#locale.DAY_2" title="Link to this definition">¶</a></dt>474<dt class="sig sig-object py" id="locale.DAY_3">475<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_3</span></span><a class="headerlink" href="#locale.DAY_3" title="Link to this definition">¶</a></dt>476<dt class="sig sig-object py" id="locale.DAY_4">477<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_4</span></span><a class="headerlink" href="#locale.DAY_4" title="Link to this definition">¶</a></dt>478<dt class="sig sig-object py" id="locale.DAY_5">479<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_5</span></span><a class="headerlink" href="#locale.DAY_5" title="Link to this definition">¶</a></dt>480<dt class="sig sig-object py" id="locale.DAY_6">481<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_6</span></span><a class="headerlink" href="#locale.DAY_6" title="Link to this definition">¶</a></dt>482<dt class="sig sig-object py" id="locale.DAY_7">483<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">DAY_7</span></span><a class="headerlink" href="#locale.DAY_7" title="Link to this definition">¶</a></dt>484<dd><p>Get the name of the n-th day of the week.</p>485<div class="admonition note">486<p class="admonition-title">Note</p>487<p>This follows the US convention of <a class="reference internal" href="#locale.DAY_1" title="locale.DAY_1"><code class="xref py py-const docutils literal notranslate"><span class="pre">DAY_1</span></code></a> being Sunday, not the488international convention (ISO 8601) that Monday is the first day of the489week.</p>490</div>491</dd></dl>492 493<dl class="py data">494<dt class="sig sig-object py" id="locale.ABDAY_1">495<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_1</span></span><a class="headerlink" href="#locale.ABDAY_1" title="Link to this definition">¶</a></dt>496<dt class="sig sig-object py" id="locale.ABDAY_2">497<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_2</span></span><a class="headerlink" href="#locale.ABDAY_2" title="Link to this definition">¶</a></dt>498<dt class="sig sig-object py" id="locale.ABDAY_3">499<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_3</span></span><a class="headerlink" href="#locale.ABDAY_3" title="Link to this definition">¶</a></dt>500<dt class="sig sig-object py" id="locale.ABDAY_4">501<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_4</span></span><a class="headerlink" href="#locale.ABDAY_4" title="Link to this definition">¶</a></dt>502<dt class="sig sig-object py" id="locale.ABDAY_5">503<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_5</span></span><a class="headerlink" href="#locale.ABDAY_5" title="Link to this definition">¶</a></dt>504<dt class="sig sig-object py" id="locale.ABDAY_6">505<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_6</span></span><a class="headerlink" href="#locale.ABDAY_6" title="Link to this definition">¶</a></dt>506<dt class="sig sig-object py" id="locale.ABDAY_7">507<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABDAY_7</span></span><a class="headerlink" href="#locale.ABDAY_7" title="Link to this definition">¶</a></dt>508<dd><p>Get the abbreviated name of the n-th day of the week.</p>509</dd></dl>510 511<dl class="py data">512<dt class="sig sig-object py" id="locale.MON_1">513<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_1</span></span><a class="headerlink" href="#locale.MON_1" title="Link to this definition">¶</a></dt>514<dt class="sig sig-object py" id="locale.MON_2">515<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_2</span></span><a class="headerlink" href="#locale.MON_2" title="Link to this definition">¶</a></dt>516<dt class="sig sig-object py" id="locale.MON_3">517<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_3</span></span><a class="headerlink" href="#locale.MON_3" title="Link to this definition">¶</a></dt>518<dt class="sig sig-object py" id="locale.MON_4">519<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_4</span></span><a class="headerlink" href="#locale.MON_4" title="Link to this definition">¶</a></dt>520<dt class="sig sig-object py" id="locale.MON_5">521<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_5</span></span><a class="headerlink" href="#locale.MON_5" title="Link to this definition">¶</a></dt>522<dt class="sig sig-object py" id="locale.MON_6">523<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_6</span></span><a class="headerlink" href="#locale.MON_6" title="Link to this definition">¶</a></dt>524<dt class="sig sig-object py" id="locale.MON_7">525<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_7</span></span><a class="headerlink" href="#locale.MON_7" title="Link to this definition">¶</a></dt>526<dt class="sig sig-object py" id="locale.MON_8">527<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_8</span></span><a class="headerlink" href="#locale.MON_8" title="Link to this definition">¶</a></dt>528<dt class="sig sig-object py" id="locale.MON_9">529<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_9</span></span><a class="headerlink" href="#locale.MON_9" title="Link to this definition">¶</a></dt>530<dt class="sig sig-object py" id="locale.MON_10">531<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_10</span></span><a class="headerlink" href="#locale.MON_10" title="Link to this definition">¶</a></dt>532<dt class="sig sig-object py" id="locale.MON_11">533<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_11</span></span><a class="headerlink" href="#locale.MON_11" title="Link to this definition">¶</a></dt>534<dt class="sig sig-object py" id="locale.MON_12">535<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">MON_12</span></span><a class="headerlink" href="#locale.MON_12" title="Link to this definition">¶</a></dt>536<dd><p>Get the name of the n-th month.</p>537</dd></dl>538 539<dl class="py data">540<dt class="sig sig-object py" id="locale.ABMON_1">541<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_1</span></span><a class="headerlink" href="#locale.ABMON_1" title="Link to this definition">¶</a></dt>542<dt class="sig sig-object py" id="locale.ABMON_2">543<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_2</span></span><a class="headerlink" href="#locale.ABMON_2" title="Link to this definition">¶</a></dt>544<dt class="sig sig-object py" id="locale.ABMON_3">545<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_3</span></span><a class="headerlink" href="#locale.ABMON_3" title="Link to this definition">¶</a></dt>546<dt class="sig sig-object py" id="locale.ABMON_4">547<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_4</span></span><a class="headerlink" href="#locale.ABMON_4" title="Link to this definition">¶</a></dt>548<dt class="sig sig-object py" id="locale.ABMON_5">549<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_5</span></span><a class="headerlink" href="#locale.ABMON_5" title="Link to this definition">¶</a></dt>550<dt class="sig sig-object py" id="locale.ABMON_6">551<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_6</span></span><a class="headerlink" href="#locale.ABMON_6" title="Link to this definition">¶</a></dt>552<dt class="sig sig-object py" id="locale.ABMON_7">553<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_7</span></span><a class="headerlink" href="#locale.ABMON_7" title="Link to this definition">¶</a></dt>554<dt class="sig sig-object py" id="locale.ABMON_8">555<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_8</span></span><a class="headerlink" href="#locale.ABMON_8" title="Link to this definition">¶</a></dt>556<dt class="sig sig-object py" id="locale.ABMON_9">557<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_9</span></span><a class="headerlink" href="#locale.ABMON_9" title="Link to this definition">¶</a></dt>558<dt class="sig sig-object py" id="locale.ABMON_10">559<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_10</span></span><a class="headerlink" href="#locale.ABMON_10" title="Link to this definition">¶</a></dt>560<dt class="sig sig-object py" id="locale.ABMON_11">561<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_11</span></span><a class="headerlink" href="#locale.ABMON_11" title="Link to this definition">¶</a></dt>562<dt class="sig sig-object py" id="locale.ABMON_12">563<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ABMON_12</span></span><a class="headerlink" href="#locale.ABMON_12" title="Link to this definition">¶</a></dt>564<dd><p>Get the abbreviated name of the n-th month.</p>565</dd></dl>566 567<dl class="py data">568<dt class="sig sig-object py" id="locale.RADIXCHAR">569<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">RADIXCHAR</span></span><a class="headerlink" href="#locale.RADIXCHAR" title="Link to this definition">¶</a></dt>570<dd><p>Get the radix character (decimal dot, decimal comma, etc.).</p>571</dd></dl>572 573<dl class="py data">574<dt class="sig sig-object py" id="locale.THOUSEP">575<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">THOUSEP</span></span><a class="headerlink" href="#locale.THOUSEP" title="Link to this definition">¶</a></dt>576<dd><p>Get the separator character for thousands (groups of three digits).</p>577</dd></dl>578 579<dl class="py data">580<dt class="sig sig-object py" id="locale.YESEXPR">581<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">YESEXPR</span></span><a class="headerlink" href="#locale.YESEXPR" title="Link to this definition">¶</a></dt>582<dd><p>Get a regular expression that can be used with the regex function to583recognize a positive response to a yes/no question.</p>584</dd></dl>585 586<dl class="py data">587<dt class="sig sig-object py" id="locale.NOEXPR">588<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">NOEXPR</span></span><a class="headerlink" href="#locale.NOEXPR" title="Link to this definition">¶</a></dt>589<dd><p>Get a regular expression that can be used with the <code class="docutils literal notranslate"><span class="pre">regex(3)</span></code> function to590recognize a negative response to a yes/no question.</p>591<div class="admonition note">592<p class="admonition-title">Note</p>593<p>The regular expressions for <a class="reference internal" href="#locale.YESEXPR" title="locale.YESEXPR"><code class="xref py py-const docutils literal notranslate"><span class="pre">YESEXPR</span></code></a> and594<a class="reference internal" href="#locale.NOEXPR" title="locale.NOEXPR"><code class="xref py py-const docutils literal notranslate"><span class="pre">NOEXPR</span></code></a> use syntax suitable for the595<code class="docutils literal notranslate"><span class="pre">regex</span></code> function from the C library, which might596differ from the syntax used in <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>.</p>597</div>598</dd></dl>599 600<dl class="py data">601<dt class="sig sig-object py" id="locale.CRNCYSTR">602<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">CRNCYSTR</span></span><a class="headerlink" href="#locale.CRNCYSTR" title="Link to this definition">¶</a></dt>603<dd><p>Get the currency symbol, preceded by “-” if the symbol should appear before604the value, “+” if the symbol should appear after the value, or “.” if the605symbol should replace the radix character.</p>606</dd></dl>607 608<dl class="py data">609<dt class="sig sig-object py" id="locale.ERA">610<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA</span></span><a class="headerlink" href="#locale.ERA" title="Link to this definition">¶</a></dt>611<dd><p>Get a string which describes how years are counted and displayed for612each era in a locale.</p>613<p>Most locales do not define this value. An example of a locale which does614define this value is the Japanese one. In Japan, the traditional615representation of dates includes the name of the era corresponding to the616then-emperor’s reign.</p>617<p>Normally it should not be necessary to use this value directly. Specifying618the <code class="docutils literal notranslate"><span class="pre">E</span></code> modifier in their format strings causes the <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>619function to use this information.620The format of the returned string is specified in <em>The Open Group Base621Specifications Issue 8</em>, paragraph <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap07.html#tag_07_03_05_02">7.3.5.2 LC_TIME C-Language Access</a>.</p>622</dd></dl>623 624<dl class="py data">625<dt class="sig sig-object py" id="locale.ERA_D_T_FMT">626<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA_D_T_FMT</span></span><a class="headerlink" href="#locale.ERA_D_T_FMT" title="Link to this definition">¶</a></dt>627<dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent date and time in a628locale-specific era-based way.</p>629</dd></dl>630 631<dl class="py data">632<dt class="sig sig-object py" id="locale.ERA_D_FMT">633<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA_D_FMT</span></span><a class="headerlink" href="#locale.ERA_D_FMT" title="Link to this definition">¶</a></dt>634<dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent a date in a635locale-specific era-based way.</p>636</dd></dl>637 638<dl class="py data">639<dt class="sig sig-object py" id="locale.ERA_T_FMT">640<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ERA_T_FMT</span></span><a class="headerlink" href="#locale.ERA_T_FMT" title="Link to this definition">¶</a></dt>641<dd><p>Get a format string for <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> to represent a time in a642locale-specific era-based way.</p>643</dd></dl>644 645<dl class="py data">646<dt class="sig sig-object py" id="locale.ALT_DIGITS">647<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">ALT_DIGITS</span></span><a class="headerlink" href="#locale.ALT_DIGITS" title="Link to this definition">¶</a></dt>648<dd><p>Get a string consisting of up to 100 semicolon-separated symbols used649to represent the values 0 to 99 in a locale-specific way.650In most locales this is an empty string.</p>651</dd></dl>652 653<p>The function temporarily sets the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> locale to the locale654of the category that determines the requested value (<code class="docutils literal notranslate"><span class="pre">LC_TIME</span></code>,655<code class="docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code>, <code class="docutils literal notranslate"><span class="pre">LC_MONETARY</span></code> or <code class="docutils literal notranslate"><span class="pre">LC_MESSAGES</span></code>) if locales are656different and the resulting string is non-ASCII.657This temporary change affects other threads.</p>658<div class="versionchanged">659<p><span class="versionmodified changed">Changed in version 3.14: </span>The function now temporarily sets the <code class="docutils literal notranslate"><span class="pre">LC_CTYPE</span></code> locale in some cases.</p>660</div>661</dd></dl>662 663<dl class="py function">664<dt class="sig sig-object py" id="locale.getdefaultlocale">665<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getdefaultlocale</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">envvars</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#locale.getdefaultlocale" title="Link to this definition">¶</a></dt>666<dd><p>Tries to determine the default locale settings and returns them as a tuple of667the form <code class="docutils literal notranslate"><span class="pre">(language</span> <span class="pre">code,</span> <span class="pre">encoding)</span></code>.</p>668<p>According to POSIX, a program which has not called <code class="docutils literal notranslate"><span class="pre">setlocale(LC_ALL,</span> <span class="pre">'')</span></code>669runs using the portable <code class="docutils literal notranslate"><span class="pre">'C'</span></code> locale. Calling <code class="docutils literal notranslate"><span class="pre">setlocale(LC_ALL,</span> <span class="pre">'')</span></code> lets670it use the default locale as defined by the <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code> variable. Since we671do not want to interfere with the current locale setting we thus emulate the672behavior in the way described above.</p>673<p>To maintain compatibility with other platforms, not only the <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LANG</span></code>674variable is tested, but a list of variables given as envvars parameter. The675first found to be defined will be used. <em>envvars</em> defaults to the search676path used in GNU gettext; it must always contain the variable name677<code class="docutils literal notranslate"><span class="pre">'LANG'</span></code>. The GNU gettext search path contains <code class="docutils literal notranslate"><span class="pre">'LC_ALL'</span></code>,678<code class="docutils literal notranslate"><span class="pre">'LC_CTYPE'</span></code>, <code class="docutils literal notranslate"><span class="pre">'LANG'</span></code> and <code class="docutils literal notranslate"><span class="pre">'LANGUAGE'</span></code>, in that order.</p>679<p>The language code has the same format as a <a class="reference internal" href="#locale-name"><span class="std std-ref">locale name</span></a>,680but without encoding and <code class="docutils literal notranslate"><span class="pre">@</span></code>-modifier.681The language code and encoding may be <code class="docutils literal notranslate"><span class="pre">None</span></code> if their values cannot be682determined.683The “C” locale is represented as <code class="docutils literal notranslate"><span class="pre">(None,</span> <span class="pre">None)</span></code>.</p>684</dd></dl>685 686<dl class="py function">687<dt class="sig sig-object py" id="locale.getlocale">688<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getlocale</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">category</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">LC_CTYPE</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.getlocale" title="Link to this definition">¶</a></dt>689<dd><p>Returns the current setting for the given locale category as a tuple containing690the language code and encoding. <em>category</em> may be one of the <code class="xref py py-const docutils literal notranslate"><span class="pre">LC_*</span></code>691values except <a class="reference internal" href="#locale.LC_ALL" title="locale.LC_ALL"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_ALL</span></code></a>. It defaults to <a class="reference internal" href="#locale.LC_CTYPE" title="locale.LC_CTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_CTYPE</span></code></a>.</p>692<p>The language code has the same format as a <a class="reference internal" href="#locale-name"><span class="std std-ref">locale name</span></a>,693but without encoding.694The language code and encoding may be <code class="docutils literal notranslate"><span class="pre">None</span></code> if their values cannot be695determined.696The “C” locale is represented as <code class="docutils literal notranslate"><span class="pre">(None,</span> <span class="pre">None)</span></code>.</p>697<div class="versionchanged">698<p><span class="versionmodified changed">Changed in version 3.15: </span><code class="docutils literal notranslate"><span class="pre">@</span></code>-modifier are no longer silently removed, but included in699the language code.</p>700</div>701</dd></dl>702 703<dl class="py function">704<dt class="sig sig-object py" id="locale.getpreferredencoding">705<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getpreferredencoding</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">do_setlocale</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.getpreferredencoding" title="Link to this definition">¶</a></dt>706<dd><p>Return the <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a> used for text data, according to user707preferences. User preferences are expressed differently on different708systems, and might not be available programmatically on some systems, so709this function only returns a guess.</p>710<p>On some systems, it is necessary to invoke <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> to obtain the711user preferences, so this function is not thread-safe. If invoking setlocale712is not necessary or desired, <em>do_setlocale</em> should be set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>713<p>On Android or if the <a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled, always714return <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code>, the <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a> and the <em>do_setlocale</em>715argument are ignored.</p>716<p>The <a class="reference internal" href="../c-api/init_config.html#c-preinit"><span class="std std-ref">Python preinitialization</span></a> configures the LC_CTYPE717locale. See also the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p>718<div class="versionchanged">719<p><span class="versionmodified changed">Changed in version 3.7: </span>The function now always returns <code class="docutils literal notranslate"><span class="pre">"utf-8"</span></code> on Android or if the720<a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled.</p>721</div>722</dd></dl>723 724<dl class="py function">725<dt class="sig sig-object py" id="locale.getencoding">726<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">getencoding</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#locale.getencoding" title="Link to this definition">¶</a></dt>727<dd><p>Get the current <a class="reference internal" href="../glossary.html#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a>:</p>728<ul class="simple">729<li><p>On Android and VxWorks, return <code class="docutils literal notranslate"><span class="pre">"utf-8"</span></code>.</p></li>730<li><p>On Unix, return the encoding of the current <a class="reference internal" href="#locale.LC_CTYPE" title="locale.LC_CTYPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">LC_CTYPE</span></code></a> locale.731Return <code class="docutils literal notranslate"><span class="pre">"utf-8"</span></code> if <code class="docutils literal notranslate"><span class="pre">nl_langinfo(CODESET)</span></code> returns an empty string:732for example, if the current LC_CTYPE locale is not supported.</p></li>733<li><p>On Windows, return the ANSI code page.</p></li>734</ul>735<p>The <a class="reference internal" href="../c-api/init_config.html#c-preinit"><span class="std std-ref">Python preinitialization</span></a> configures the LC_CTYPE736locale. See also the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p>737<p>This function is similar to738<a class="reference internal" href="#locale.getpreferredencoding" title="locale.getpreferredencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">getpreferredencoding(False)</span></code></a> except this739function ignores the <a class="reference internal" href="os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a>.</p>740<div class="versionadded">741<p><span class="versionmodified added">Added in version 3.11.</span></p>742</div>743</dd></dl>744 745<dl class="py function">746<dt class="sig sig-object py" id="locale.normalize">747<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">normalize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">localename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.normalize" title="Link to this definition">¶</a></dt>748<dd><p>Returns a normalized locale code for the given locale name. The returned locale749code is formatted for use with <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a>. If normalization fails, the750original name is returned unchanged.</p>751<p>If the given encoding is not known, the function defaults to the default752encoding for the locale code just like <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a>.</p>753</dd></dl>754 755<dl class="py function">756<dt class="sig sig-object py" id="locale.strcoll">757<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">strcoll</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">string2</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.strcoll" title="Link to this definition">¶</a></dt>758<dd><p>Compares two strings according to the current <a class="reference internal" href="#locale.LC_COLLATE" title="locale.LC_COLLATE"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_COLLATE</span></code></a> setting. As759any other compare function, returns a negative, or a positive value, or <code class="docutils literal notranslate"><span class="pre">0</span></code>,760depending on whether <em>string1</em> collates before or after <em>string2</em> or is equal to761it.</p>762</dd></dl>763 764<dl class="py function">765<dt class="sig sig-object py" id="locale.strxfrm">766<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">strxfrm</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.strxfrm" title="Link to this definition">¶</a></dt>767<dd><p>Transforms a string to one that can be used in locale-aware768comparisons. For example, <code class="docutils literal notranslate"><span class="pre">strxfrm(s1)</span> <span class="pre"><</span> <span class="pre">strxfrm(s2)</span></code> is769equivalent to <code class="docutils literal notranslate"><span class="pre">strcoll(s1,</span> <span class="pre">s2)</span> <span class="pre"><</span> <span class="pre">0</span></code>. This function can be used770when the same string is compared repeatedly, e.g. when collating a771sequence of strings.</p>772</dd></dl>773 774<dl class="py function">775<dt class="sig sig-object py" id="locale.format_string">776<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">format_string</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">val</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grouping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">monetary</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.format_string" title="Link to this definition">¶</a></dt>777<dd><p>Formats a number <em>val</em> according to the current <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> setting.778The format follows the conventions of the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator. For floating-point779values, the decimal point is modified if appropriate. If <em>grouping</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>,780also takes the grouping into account.</p>781<p>If <em>monetary</em> is true, the conversion uses monetary thousands separator and782grouping strings.</p>783<p>Processes formatting specifiers as in <code class="docutils literal notranslate"><span class="pre">format</span> <span class="pre">%</span> <span class="pre">val</span></code>, but takes the current784locale settings into account.</p>785<div class="versionchanged">786<p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>monetary</em> keyword parameter was added.</p>787</div>788</dd></dl>789 790<dl class="py function">791<dt class="sig sig-object py" id="locale.currency">792<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">currency</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">val</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symbol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grouping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">international</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.currency" title="Link to this definition">¶</a></dt>793<dd><p>Formats a number <em>val</em> according to the current <a class="reference internal" href="#locale.LC_MONETARY" title="locale.LC_MONETARY"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_MONETARY</span></code></a> settings.</p>794<p>The returned string includes the currency symbol if <em>symbol</em> is true, which is795the default. If <em>grouping</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (which is not the default), grouping is done796with the value. If <em>international</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (which is not the default), the797international currency symbol is used.</p>798<div class="admonition note">799<p class="admonition-title">Note</p>800<p>This function will not work with the ‘C’ locale, so you have to set a801locale via <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> first.</p>802</div>803</dd></dl>804 805<dl class="py function">806<dt class="sig sig-object py" id="locale.str">807<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">str</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">float</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.str" title="Link to this definition">¶</a></dt>808<dd><p>Formats a floating-point number using the same format as the built-in function809<code class="docutils literal notranslate"><span class="pre">str(float)</span></code>, but takes the decimal point into account.</p>810</dd></dl>811 812<dl class="py function">813<dt class="sig sig-object py" id="locale.delocalize">814<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">delocalize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.delocalize" title="Link to this definition">¶</a></dt>815<dd><p>Converts a string into a normalized number string, following the816<a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> settings.</p>817<div class="versionadded">818<p><span class="versionmodified added">Added in version 3.5.</span></p>819</div>820</dd></dl>821 822<dl class="py function">823<dt class="sig sig-object py" id="locale.localize">824<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">localize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grouping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">monetary</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.localize" title="Link to this definition">¶</a></dt>825<dd><p>Converts a normalized number string into a formatted string following the826<a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> settings.</p>827<div class="versionadded">828<p><span class="versionmodified added">Added in version 3.10.</span></p>829</div>830</dd></dl>831 832<dl class="py function">833<dt class="sig sig-object py" id="locale.atof">834<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">atof</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">float</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.atof" title="Link to this definition">¶</a></dt>835<dd><p>Converts a string to a number, following the <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> settings,836by calling <em>func</em> on the result of calling <a class="reference internal" href="#locale.delocalize" title="locale.delocalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">delocalize()</span></code></a> on <em>string</em>.</p>837</dd></dl>838 839<dl class="py function">840<dt class="sig sig-object py" id="locale.atoi">841<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">atoi</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.atoi" title="Link to this definition">¶</a></dt>842<dd><p>Converts a string to an integer, following the <a class="reference internal" href="#locale.LC_NUMERIC" title="locale.LC_NUMERIC"><code class="xref py py-const docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code></a> conventions.</p>843</dd></dl>844 845<dl class="py data">846<dt class="sig sig-object py" id="locale.LC_CTYPE">847<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_CTYPE</span></span><a class="headerlink" href="#locale.LC_CTYPE" title="Link to this definition">¶</a></dt>848<dd><p>Locale category for the character type functions. Most importantly, this849category defines the text encoding, i.e. how bytes are interpreted as850Unicode codepoints. See <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0538/"><strong>PEP 538</strong></a> and <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0540/"><strong>PEP 540</strong></a> for how this variable851might be automatically coerced to <code class="docutils literal notranslate"><span class="pre">C.UTF-8</span></code> to avoid issues created by852invalid settings in containers or incompatible settings passed over remote853SSH connections.</p>854<p>Python doesn’t internally use locale-dependent character transformation functions855from <code class="docutils literal notranslate"><span class="pre">ctype.h</span></code>. Instead, <code class="docutils literal notranslate"><span class="pre">pyctype.h</span></code> provides locale-independent856equivalents like <a class="reference internal" href="../c-api/conversion.html#c.Py_TOLOWER" title="Py_TOLOWER"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TOLOWER</span></code></a>.</p>857</dd></dl>858 859<dl class="py data">860<dt class="sig sig-object py" id="locale.LC_COLLATE">861<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_COLLATE</span></span><a class="headerlink" href="#locale.LC_COLLATE" title="Link to this definition">¶</a></dt>862<dd><p>Locale category for sorting strings. The functions <a class="reference internal" href="#locale.strcoll" title="locale.strcoll"><code class="xref py py-func docutils literal notranslate"><span class="pre">strcoll()</span></code></a> and863<a class="reference internal" href="#locale.strxfrm" title="locale.strxfrm"><code class="xref py py-func docutils literal notranslate"><span class="pre">strxfrm()</span></code></a> of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> module are affected.</p>864</dd></dl>865 866<dl class="py data">867<dt class="sig sig-object py" id="locale.LC_TIME">868<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_TIME</span></span><a class="headerlink" href="#locale.LC_TIME" title="Link to this definition">¶</a></dt>869<dd><p>Locale category for the formatting of time. The function <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>870follows these conventions.</p>871</dd></dl>872 873<dl class="py data">874<dt class="sig sig-object py" id="locale.LC_MONETARY">875<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_MONETARY</span></span><a class="headerlink" href="#locale.LC_MONETARY" title="Link to this definition">¶</a></dt>876<dd><p>Locale category for formatting of monetary values. The available options are877available from the <a class="reference internal" href="#locale.localeconv" title="locale.localeconv"><code class="xref py py-func docutils literal notranslate"><span class="pre">localeconv()</span></code></a> function.</p>878</dd></dl>879 880<dl class="py data">881<dt class="sig sig-object py" id="locale.LC_MESSAGES">882<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_MESSAGES</span></span><a class="headerlink" href="#locale.LC_MESSAGES" title="Link to this definition">¶</a></dt>883<dd><p>Locale category for message display. Python currently does not support884application specific locale-aware messages. Messages displayed by the operating885system, like those returned by <a class="reference internal" href="os.html#os.strerror" title="os.strerror"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.strerror()</span></code></a> might be affected by this886category.</p>887<p>This value may not be available on operating systems not conforming to the888POSIX standard, most notably Windows.</p>889</dd></dl>890 891<dl class="py data">892<dt class="sig sig-object py" id="locale.LC_NUMERIC">893<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_NUMERIC</span></span><a class="headerlink" href="#locale.LC_NUMERIC" title="Link to this definition">¶</a></dt>894<dd><p>Locale category for formatting numbers. The functions <a class="reference internal" href="#locale.format_string" title="locale.format_string"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_string()</span></code></a>,895<a class="reference internal" href="#locale.atoi" title="locale.atoi"><code class="xref py py-func docutils literal notranslate"><span class="pre">atoi()</span></code></a>, <a class="reference internal" href="#locale.atof" title="locale.atof"><code class="xref py py-func docutils literal notranslate"><span class="pre">atof()</span></code></a> and <a class="reference internal" href="#locale.str" title="locale.str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> module are896affected by that category. All other numeric formatting operations are not897affected.</p>898</dd></dl>899 900<dl class="py data">901<dt class="sig sig-object py" id="locale.LC_ALL">902<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">LC_ALL</span></span><a class="headerlink" href="#locale.LC_ALL" title="Link to this definition">¶</a></dt>903<dd><p>Combination of all locale settings. If this flag is used when the locale is904changed, setting the locale for all categories is attempted. If that fails for905any category, no category is changed at all. When the locale is retrieved using906this flag, a string indicating the setting for all categories is returned. This907string can be later used to restore the settings.</p>908</dd></dl>909 910<dl class="py data">911<dt class="sig sig-object py" id="locale.CHAR_MAX">912<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">CHAR_MAX</span></span><a class="headerlink" href="#locale.CHAR_MAX" title="Link to this definition">¶</a></dt>913<dd><p>This is a symbolic constant used for different values returned by914<a class="reference internal" href="#locale.localeconv" title="locale.localeconv"><code class="xref py py-func docutils literal notranslate"><span class="pre">localeconv()</span></code></a>.</p>915</dd></dl>916 917<section id="background-details-hints-tips-and-caveats">918<h2>Background, details, hints, tips and caveats<a class="headerlink" href="#background-details-hints-tips-and-caveats" title="Link to this heading">¶</a></h2>919<p>The C standard defines the locale as a program-wide property that may be920relatively expensive to change. On top of that, some implementations are broken921in such a way that frequent locale changes may cause core dumps. This makes the922locale somewhat painful to use correctly.</p>923<p>Initially, when a program is started, the locale is the <code class="docutils literal notranslate"><span class="pre">C</span></code> locale, no matter924what the user’s preferred locale is. There is one exception: the925<a class="reference internal" href="#locale.LC_CTYPE" title="locale.LC_CTYPE"><code class="xref py py-data docutils literal notranslate"><span class="pre">LC_CTYPE</span></code></a> category is changed at startup to set the current locale926encoding to the user’s preferred locale encoding. The program must explicitly927say that it wants the user’s preferred locale settings for other categories by928calling <code class="docutils literal notranslate"><span class="pre">setlocale(LC_ALL,</span> <span class="pre">'')</span></code>.</p>929<p>It is generally a bad idea to call <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a> in some library routine,930since as a side effect it affects the entire program. Saving and restoring it931is almost as bad: it is expensive and affects other threads that happen to run932before the settings have been restored.</p>933<p>If, when coding a module for general use, you need a locale independent version934of an operation that is affected by the locale (such as935certain formats used with <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>), you will have to find a way to936do it without using the standard library routine. Even better is convincing937yourself that using locale settings is okay. Only as a last resort should you938document that your module is not compatible with non-<code class="docutils literal notranslate"><span class="pre">C</span></code> locale settings.</p>939<p>The only way to perform numeric operations according to the locale is to use the940special functions defined by this module: <a class="reference internal" href="#locale.atof" title="locale.atof"><code class="xref py py-func docutils literal notranslate"><span class="pre">atof()</span></code></a>, <a class="reference internal" href="#locale.atoi" title="locale.atoi"><code class="xref py py-func docutils literal notranslate"><span class="pre">atoi()</span></code></a>,941<a class="reference internal" href="#locale.format_string" title="locale.format_string"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_string()</span></code></a>, <a class="reference internal" href="#locale.str" title="locale.str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>.</p>942<p>There is no way to perform case conversions and character classifications943according to the locale. For (Unicode) text strings these are done according944to the character value only, while for byte strings, the conversions and945classifications are done according to the ASCII value of the byte, and bytes946whose high bit is set (i.e., non-ASCII bytes) are never converted or considered947part of a character class such as letter or whitespace.</p>948</section>949<section id="locale-names">950<span id="locale-name"></span><h2>Locale names<a class="headerlink" href="#locale-names" title="Link to this heading">¶</a></h2>951<p>The format of the locale name is platform dependent, and the set of supported952locales can depend on the system configuration.</p>953<p>On Posix platforms, it usually has the format <a class="footnote-reference brackets" href="#id4" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a>:</p>954<pre class="highlight">955 language [<span class="sx">"_"</span> territory] [<span class="sx">"."</span> charset] [<span class="sx">"@"</span> modifier]956</pre>957<p>where <em>language</em> is a two- or three-letter language code from <a class="reference external" href="https://www.iso.org/iso-639-language-code">ISO 639</a>,958<em>territory</em> is a two-letter country or region code from <a class="reference external" href="https://www.iso.org/iso-3166-country-codes.html">ISO 3166</a>,959<em>charset</em> is a locale encoding, and <em>modifier</em> is a script name,960a language subtag, a sort order identifier, or other locale modifier961(for example, “latin”, “valencia”, “stroke” and “euro”).</p>962<p>On Windows, several formats are supported. <a class="footnote-reference brackets" href="#id5" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id6" id="id3" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a>963A subset of <a class="reference external" href="https://www.rfc-editor.org/info/bcp47">IETF BCP 47</a> tags:</p>964<pre class="highlight">965 language [<span class="sx">"-"</span> script] [<span class="sx">"-"</span> territory] [<span class="sx">"."</span> charset]966 language [<span class="sx">"-"</span> script] <span class="sx">"-"</span> territory <span class="sx">"-"</span> modifier967</pre>968<p>where <em>language</em> and <em>territory</em> have the same meaning as in Posix,969<em>script</em> is a four-letter script code from <a class="reference external" href="https://www.unicode.org/iso15924/">ISO 15924</a>,970and <em>modifier</em> is a language subtag, a sort order identifier971or custom modifier (for example, “valencia”, “stroke” or “x-python”).972Both hyphen (<code class="docutils literal notranslate"><span class="pre">'-'</span></code>) and underscore (<code class="docutils literal notranslate"><span class="pre">'_'</span></code>) separators are supported.973Only UTF-8 encoding is allowed for BCP 47 tags.</p>974<p>Windows also supports locale names in the format:</p>975<pre class="highlight">976 language [<span class="sx">"_"</span> territory] [<span class="sx">"."</span> charset]977</pre>978<p>where <em>language</em> and <em>territory</em> are full names, such as “English” and979“United States”, and <em>charset</em> is either a code page number (for example, “1252”)980or UTF-8.981Only the underscore separator is supported in this format.</p>982<p>The “C” locale is supported on all platforms.</p>983<aside class="footnote-list brackets">984<aside class="footnote brackets" id="id4" role="doc-footnote">985<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>986<p><a class="reference external" href="https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap08.html#tag_08_02">IEEE Std 1003.1-2024; 8.2 Internationalization Variables</a></p>987</aside>988<aside class="footnote brackets" id="id5" role="doc-footnote">989<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id2">2</a><span class="fn-bracket">]</span></span>990<p><a class="reference external" href="https://learn.microsoft.com/en-us/cpp/c-runtime-library/locale-names-languages-and-country-region-strings">UCRT Locale names, Languages, and Country/Region strings</a></p>991</aside>992<aside class="footnote brackets" id="id6" role="doc-footnote">993<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id3">3</a><span class="fn-bracket">]</span></span>994<p><a class="reference external" href="https://learn.microsoft.com/en-us/windows/win32/intl/locale-names">Locale Names</a></p>995</aside>996</aside>997</section>998<section id="for-extension-writers-and-programs-that-embed-python">999<span id="embedding-locale"></span><h2>For extension writers and programs that embed Python<a class="headerlink" href="#for-extension-writers-and-programs-that-embed-python" title="Link to this heading">¶</a></h2>1000<p>Extension modules should never call <a class="reference internal" href="#locale.setlocale" title="locale.setlocale"><code class="xref py py-func docutils literal notranslate"><span class="pre">setlocale()</span></code></a>, except to find out what1001the current locale is. But since the return value can only be used portably to1002restore it, that is not very useful (except perhaps to find out whether or not1003the locale is <code class="docutils literal notranslate"><span class="pre">C</span></code>).</p>1004<p>When Python code uses the <code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> module to change the locale, this also1005affects the embedding application. If the embedding application doesn’t want1006this to happen, it should remove the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_locale</span></code> extension module (which does1007all the work) from the table of built-in modules in the <code class="file docutils literal notranslate"><span class="pre">config.c</span></code> file,1008and make sure that the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_locale</span></code> module is not accessible as a shared1009library.</p>1010</section>1011<section id="access-to-message-catalogs">1012<span id="locale-gettext"></span><h2>Access to message catalogs<a class="headerlink" href="#access-to-message-catalogs" title="Link to this heading">¶</a></h2>1013<dl class="py function">1014<dt class="sig sig-object py" id="locale.gettext">1015<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">gettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.gettext" title="Link to this definition">¶</a></dt>1016<dd></dd></dl>1017 1018<dl class="py function">1019<dt class="sig sig-object py" id="locale.dgettext">1020<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">dgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.dgettext" title="Link to this definition">¶</a></dt>1021<dd></dd></dl>1022 1023<dl class="py function">1024<dt class="sig sig-object py" id="locale.dcgettext">1025<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">dcgettext</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">category</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.dcgettext" title="Link to this definition">¶</a></dt>1026<dd></dd></dl>1027 1028<dl class="py function">1029<dt class="sig sig-object py" id="locale.textdomain">1030<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">textdomain</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.textdomain" title="Link to this definition">¶</a></dt>1031<dd></dd></dl>1032 1033<dl class="py function">1034<dt class="sig sig-object py" id="locale.bindtextdomain">1035<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">bindtextdomain</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.bindtextdomain" title="Link to this definition">¶</a></dt>1036<dd></dd></dl>1037 1038<dl class="py function">1039<dt class="sig sig-object py" id="locale.bind_textdomain_codeset">1040<span class="sig-prename descclassname"><span class="pre">locale.</span></span><span class="sig-name descname"><span class="pre">bind_textdomain_codeset</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">domain</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">codeset</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#locale.bind_textdomain_codeset" title="Link to this definition">¶</a></dt>1041<dd></dd></dl>1042 1043<p>The locale module exposes the C library’s gettext interface on systems that1044provide this interface. It consists of the functions <a class="reference internal" href="gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext()</span></code></a>,1045<a class="reference internal" href="#locale.dgettext" title="locale.dgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dgettext()</span></code></a>, <a class="reference internal" href="#locale.dcgettext" title="locale.dcgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">dcgettext()</span></code></a>, <a class="reference internal" href="#locale.textdomain" title="locale.textdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">textdomain()</span></code></a>, <a class="reference internal" href="#locale.bindtextdomain" title="locale.bindtextdomain"><code class="xref py py-func docutils literal notranslate"><span class="pre">bindtextdomain()</span></code></a>,1046and <a class="reference internal" href="#locale.bind_textdomain_codeset" title="locale.bind_textdomain_codeset"><code class="xref py py-func docutils literal notranslate"><span class="pre">bind_textdomain_codeset()</span></code></a>. These are similar to the same functions in1047the <a class="reference internal" href="gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> module, but use the C library’s binary format for message1048catalogs, and the C library’s search algorithms for locating message catalogs.</p>1049<p>Python applications should normally find no need to invoke these functions, and1050should use <a class="reference internal" href="gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> instead. A known exception to this rule are1051applications that link with additional C libraries which internally invoke1052C functions <code class="docutils literal notranslate"><span class="pre">gettext</span></code> or <code class="docutils literal notranslate"><span class="pre">dcgettext</span></code>. For these applications, it may be1053necessary to bind the text domain, so that the libraries can properly locate1054their message catalogs.</p>1055</section>1056</section>1057 1058 1059 <div class="clearer"></div>1060 </div>1061 </div>1062 </div>1063 <div class="sphinxsidebar" role="navigation" aria-label="Main">1064 <div class="sphinxsidebarwrapper">1065 <div>1066 <h3><a href="../contents.html">Table of Contents</a></h3>1067 <ul>1068<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a><ul>1069<li><a class="reference internal" href="#background-details-hints-tips-and-caveats">Background, details, hints, tips and caveats</a></li>1070<li><a class="reference internal" href="#locale-names">Locale names</a></li>1071<li><a class="reference internal" href="#for-extension-writers-and-programs-that-embed-python">For extension writers and programs that embed Python</a></li>1072<li><a class="reference internal" href="#access-to-message-catalogs">Access to message catalogs</a></li>1073</ul>1074</li>1075</ul>1076 1077 </div>1078 <div>1079 <h4>Previous topic</h4>1080 <p class="topless"><a href="gettext.html"1081 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code> — Multilingual internationalization services</a></p>1082 </div>1083 <div>1084 <h4>Next topic</h4>1085 <p class="topless"><a href="tk.html"1086 title="next chapter">Graphical user interfaces with Tk</a></p>1087 </div>1088 <script>1089 document.addEventListener('DOMContentLoaded', () => {1090 const title = document.querySelector('meta[property="og:title"]').content;1091 const elements = document.querySelectorAll('.improvepage');1092 const pageurl = window.location.href.split('?')[0];1093 elements.forEach(element => {1094 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));1095 url.searchParams.set('pagetitle', title);1096 url.searchParams.set('pageurl', pageurl);1097 url.searchParams.set('pagesource', "library/locale.rst");1098 element.href = url.toString();1099 });1100 });1101 </script>1102 <div role="note" aria-label="source link">1103 <h3>This page</h3>1104 <ul class="this-page-menu">1105 <li><a href="../bugs.html">Report a bug</a></li>1106 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>1107 <li>1108 <a href="https://github.com/python/cpython/blob/main/Doc/library/locale.rst?plain=1"1109 rel="nofollow">Show source1110 </a>1111 </li>1112 1113 </ul>1114 </div>1115 </div>1116<div id="sidebarbutton" title="Collapse sidebar">1117<span>«</span>1118</div>1119 1120 </div>1121 <div class="clearer"></div>1122 </div> 1123 <div class="related" role="navigation" aria-label="Related">1124 <h3>Navigation</h3>1125 <ul>1126 <li class="right" style="margin-right: 10px">1127 <a href="../genindex.html" title="General Index"1128 >index</a></li>1129 <li class="right" >1130 <a href="../py-modindex.html" title="Python Module Index"1131 >modules</a> |</li>1132 <li class="right" >1133 <a href="tk.html" title="Graphical user interfaces with Tk"1134 >next</a> |</li>1135 <li class="right" >1136 <a href="gettext.html" title="gettext — Multilingual internationalization services"1137 >previous</a> |</li>1138 1139 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>1140 <li><a href="https://www.python.org/">Python</a> »</li>1141 <li class="switchers">1142 <div class="language_switcher_placeholder"></div>1143 <div class="version_switcher_placeholder"></div>1144 </li>1145 <li>1146 1147 </li>1148 <li id="cpython-language-and-version">1149 <a href="../index.html">3.15.0a6 Documentation</a> »1150 </li>1151 1152 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>1153 <li class="nav-item nav-item-2"><a href="i18n.html" >Internationalization</a> »</li>1154 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code> — Internationalization services</a></li>1155 <li class="right">1156 1157 1158 <div class="inline-search" role="search">1159 <form class="inline-search" action="../search.html" method="get">1160 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">1161 <input type="submit" value="Go">1162 </form>1163 </div>1164 |1165 </li>1166 <li class="right">1167<label class="theme-selector-label">1168 Theme1169 <select class="theme-selector" oninput="activateTheme(this.value)">1170 <option value="auto" selected>Auto</option>1171 <option value="light">Light</option>1172 <option value="dark">Dark</option>1173 </select>1174</label> |</li>1175 1176 </ul>1177 </div> 1178 <div class="footer">1179 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.1180 <br>1181 This page is licensed under the Python Software Foundation License Version 2.1182 <br>1183 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.1184 <br>1185 1186 See <a href="/license.html">History and License</a> for more information.<br>1187 1188 1189 <br>1190 1191 The Python Software Foundation is a non-profit corporation.1192<a href="https://www.python.org/psf/donations/">Please donate.</a>1193<br>1194 <br>1195 Last updated on Mar 10, 2026 (08:58 UTC).1196 1197 <a href="/bugs.html">Found a bug</a>?1198 1199 <br>1200 