{"id":5371,"date":"2020-04-28T19:06:49","date_gmt":"2020-04-28T19:06:49","guid":{"rendered":"https:\/\/www.askpython.com\/?p=5371"},"modified":"2023-02-16T19:57:06","modified_gmt":"2023-02-16T19:57:06","slug":"python-ascii-function","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-ascii-function","title":{"rendered":"How to use the Python ascii() function"},"content":{"rendered":"\n<p>In this article, we&#8217;ll take a look at the Python <strong>ascii()<\/strong> function.<\/p>\n\n\n\n<p>The ascii() function returns a string representation of the object but only having ASCII characters as it is.<\/p>\n\n\n\n<p>The remaining non-ASCII characters will be escaped with a backslash (\\). For example, the newline character (<code>\\n<\/code>) is a non-ASCII character.<\/p>\n\n\n\n<p>We&#8217;ll now look at some examples to understand how it exactly works!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Python ascii() function &#8211; Some examples<\/h2>\n\n\n\n<p>The Python <strong>ascii()<\/strong> function takes a single argument, which can be any object. So all kinds of objects, like lists, strings, etc, are valid. This will return a string.<\/p>\n\n\n\n<p>If you&#8217;re using it on a <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">List<\/a>, or any collection, this function will get called for each member of the collection.<\/p>\n\n\n\n<p>Let&#8217;s take a look at this now.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using Python ascii() on primitive datatypes<\/h3>\n\n\n\n<p>For basic datatypes like <code>boolean<\/code>, <code>string<\/code>, <code>int<\/code>, they work as you expect.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ni = 15\nprint(ascii(i))\n\nb = True\nprint(ascii(b))\n\ns = &#039;abc&#039;\nprint(ascii(s))\n\ns = &#039;Hello from\\tAskPython\\n&#039;\nprint(ascii(s))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#039;15&#039;\n&#039;True&#039;\n&quot;&#039;abc&#039;&quot;\n&quot;&#039;Hello from\\\\tAskPython\\\\n&#039;&quot;\n<\/pre><\/div>\n\n\n<p>As you can see, for the non-ASCII characters (\\t, \\n), the backslash itself needs to be escaped.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using ascii() on Iterables\/Collections<\/h3>\n\n\n\n<p>In case you want to use it on a list\/tuple\/dictionary, you still can! But, this will simply apply it to every member in the collection\/iterable.<\/p>\n\n\n\n<p>So if a list has <code>n<\/code> elements, we&#8217;ll get the function applied to all <code>n<\/code> of them, and get back a list of strings.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nm = &#x5B;&quot;Hello from AskPython 22&quot;, &quot;AskPyth\u00f6n&quot;, &quot;Hi&quot;]\nprint(ascii(m))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;Hello from AskPython 22&#039;, &#039;AskPyth\\xf6n&#039;, &#039;Hi&#039;]\n<\/pre><\/div>\n\n\n<p>Similarly, with a Dictionary {<code>key<\/code>:<code>value<\/code>}, it&#8217;ll be applied to both <code>key<\/code> and <code>value<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = {&#039;\u00e2&#039;:&#039;\u00e5&#039;, &#039;2&#039;:2, &#039;\u00e7&#039;:&#039;\u0107&#039;}\nprint(ascii(d))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{&#039;\\xe2&#039;: &#039;\\xe5&#039;, &#039;2&#039;: 2, &#039;\\xe7&#039;: &#039;\\u0107&#039;}\n<\/pre><\/div>\n\n\n<p>For a tuple, it is similar to that of a list. All elements will be converted to a string representation of ASCII-like characters.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nt = (&quot;Hell\u00f6&quot;, 123, &#x5B;&quot;AskPython&quot;])\nprint(ascii(t))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n(&#039;Hell\\xf6&#039;, 123, &#x5B;&#039;AskPython&#039;])\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Comparison with the repr() function<\/h3>\n\n\n\n<p>The <code>repr()<\/code> function is also used to return a string representation of objects. But the difference is that <code>repr()<\/code> prints the non-ascii characters as such.<\/p>\n\n\n\n<p>For custom objects, the <code>ascii()<\/code> function internally calls the <code>__repr__()<\/code> function, but makes sure to escape non-ASCII characters.<\/p>\n\n\n\n<p>Let&#8217;s experiment with this, by creating our own object, using a class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass MyClass:\n    def __init__(self, name):\n        self.name = name\n<\/pre><\/div>\n\n\n<p>Now, let&#8217;s create an object and try to invoke <code>ascii()<\/code> and <code>repr()<\/code> on it,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_obj = MyClass(&quot;AskPyth\u00f6n&quot;)\nprint(ascii(my_obj))\nprint(repr(my_obj))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#039;&lt;__main__.MyClass object at 0x7f6adcf30940&gt;&#039;\n&#039;&lt;__main__.MyClass object at 0x7f6adcf30940&gt;&#039;\n<\/pre><\/div>\n\n\n<p>We don&#8217;t have a <code>repr()<\/code> function for this class, so the default <code>object<\/code> definition is used. That&#8217;s why you see <code>MyClass object<\/code> in the output.<\/p>\n\n\n\n<p>To change this, we must overload the <code>__repr__()<\/code> dunder method ourselves.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass MyClass:\n    def __init__(self, name):\n        self.name = name\n    def __repr__(self):\n        return self.name\n<\/pre><\/div>\n\n\n<p>Now, when you invoke <code>ascii()<\/code> or <code>repr()<\/code>, we can directly get the name attribute!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_obj = MyClass(&quot;AskPyth\u00f6n&quot;)\nprint(ascii(my_obj))\nprint(repr(my_obj))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nAskPyth\\xf6n\nAskPyth\u00f6n\n<\/pre><\/div>\n\n\n<p>Now, you can clearly see the difference!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned about using the <code>ascii()<\/code> function in Python and learned to use it on different types of objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3.8\/library\/functions.html#ascii\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python Documentation<\/a> on the ascii() function<\/li><li>JournalDev article on Python ascii()<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll take a look at the Python ascii() function. The ascii() function returns a string representation of the object but only having ASCII characters as it is. The remaining non-ASCII characters will be escaped with a backslash (\\). For example, the newline character (\\n) is a non-ASCII character. We&#8217;ll now look at [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":5373,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-5371","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=5371"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5371\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/5373"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=5371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=5371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=5371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}