<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Printing unicode characters in Python strings</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2014/02/02/Printing-unicode-characters-in-Python-strings</link>
      <pubDate>Sun, 02 Feb 2014 12:18:16 EST</pubDate>
      <category><![CDATA[unicode]]></category>
      <category><![CDATA[python]]></category>
      <guid isPermaLink="false">mK4I8mqio48oQmOrYBu--mvm_Sk=</guid>
      <description>Printing unicode characters in Python strings</description>
      <content:encoded><![CDATA[


&lt;p&gt;
Are you tired of printing strings like this:
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; &lt;span style="color: #228b22;"&gt;'The volume is {0} Angstrom^3'&lt;/span&gt;.&lt;span style="color: #cd0000;"&gt;format&lt;/span&gt;(125)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
The volume is 125 Angstrom^3
&lt;/pre&gt;

&lt;p&gt;
Wish you could get Å in your string? That is the unicode character
U+212B. We can get that to print in Python, but we have to create it
in a unicode string, and print the string properly encoded. Let us try
it out.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; u&lt;span style="color: #228b22;"&gt;'\u212B'&lt;/span&gt;.encode(&lt;span style="color: #228b22;"&gt;'utf-8'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Å
&lt;/pre&gt;

&lt;p&gt;
We use u'' to indicate a unicode string. Note we have to encode the
string to print it, or will get this error:
&lt;/p&gt;

&lt;pre class="example"&gt;
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
UnicodeEncodeError: 'ascii' codec can't encode character u'\u212b' in position 0: ordinal not in range(128)
&lt;/pre&gt;

&lt;p&gt;
Do more, do more, we wish we could! Unicode also supports some
superscripted and subscripted numbers
(&lt;a href="http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts"&gt;http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts&lt;/a&gt; ). Let
us see that in action.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; u&lt;span style="color: #228b22;"&gt;'\u212B\u00B3'&lt;/span&gt;.encode(&lt;span style="color: #228b22;"&gt;'utf-8'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Å³
&lt;/pre&gt;

&lt;p&gt;
Pretty sweet. The code is not all that readable if you aren't fluent
in unicode, but if it was buried in some library it would just print
something nice looking. We can use this to print chemical formulas
too.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; u&lt;span style="color: #228b22;"&gt;'''The chemical formula of water is H\u2082O.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;Water dissociates into H\u207A and OH\u207B'''&lt;/span&gt;.encode(&lt;span style="color: #228b22;"&gt;'utf-8'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
=The chemical formula of water is H₂O.
Water dissociates into H⁺ and OH⁻
&lt;/p&gt;

&lt;p&gt;
There are other encodings too. See the symbols here: &lt;a href="http://en.wikipedia.org/wiki/Number_Forms"&gt;http://en.wikipedia.org/wiki/Number_Forms&lt;/a&gt; 
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; u&lt;span style="color: #228b22;"&gt;'1/4 or \u00BC'&lt;/span&gt;.encode(&lt;span style="color: #228b22;"&gt;'latin-1'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
1/4 or ¼
&lt;/pre&gt;

&lt;p&gt;
That seems like:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; u&lt;span style="color: #228b22;"&gt;'A good idea\u00AE'&lt;/span&gt;.encode(&lt;span style="color: #228b22;"&gt;'latin-1'&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
A good idea®
&lt;/pre&gt;

&lt;p&gt;
I can not tell how you know exactly what encoding to use. If you use
utf-8 in the example above, you get a stray character in front of the
desired trademark symbol. Still, it is interesting you can get
prettier symbols!
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2014 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2014/02/02/Printing-unicode-characters-in-Python-strings.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.5g&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
