{"id":588,"date":"2024-10-19T07:20:42","date_gmt":"2024-10-19T07:20:42","guid":{"rendered":"https:\/\/itsmycode.com\/?p=588"},"modified":"2024-10-19T07:20:43","modified_gmt":"2024-10-19T07:20:43","slug":"python-ascii","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-ascii\/","title":{"rendered":"Python ascii()"},"content":{"rendered":"\n<p>The <strong><code>ascii()<\/code><\/strong> in Python is a built-in function that returns a printable and readable version of any object such as <strong>Strings<\/strong>, <strong>Tuples<\/strong>, <strong>Lists<\/strong>, etc. The <strong><code>ascii()<\/code><\/strong> function will escape the non-ASCII character using&nbsp;<strong>\\x, \\u or \\U escapes.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ascii-syntax\">ascii() Syntax&nbsp;<\/h2>\n\n\n\n<p>The syntax of the <strong><code>ascii()<\/code><\/strong> method is&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>ascii(object)<\/strong><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ascii-parameters\">ascii() Parameters<\/h2>\n\n\n\n<p>The <strong><code>ascii()<\/code><\/strong> function takes an object as an argument. The object can be of type <strong>Strings<\/strong>, <strong>Lists<\/strong>, <strong>Tuples<\/strong>, etc.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ascii-return-value\">ascii() Return Value<\/h2>\n\n\n\n<p>The <strong><code>ascii()<\/code><\/strong> function returns a string containing a printable representation of an object. The <strong><code>ascii()<\/code><\/strong> function will escape the non-ASCII character using&nbsp;<strong>\\x, \\u, or \\U.&nbsp;<\/strong><\/p>\n\n\n\n<p>For example, the non-ASCII characters \u201c<strong>\u00a5<\/strong>\u201d will output as <code>\\xa5<\/code> and <strong>\u221a<\/strong> will output as <strong><code>\\u221a<\/code><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-how-ascii-method-works\">Example 1: How ascii() method works?<\/h2>\n\n\n\n<p>In case if you pass multi-line text to the <strong><code>ascii()<\/code><\/strong> function, it will replace the line breaks with \u201c<strong>\\n<\/strong>\u201d as the value of the new line is \u201c<strong>\\n<\/strong>\u201d&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# Normal string \ntext = 'Hello World'\nprint(ascii(text))\n\n# Text with Non-ASCII characters\nascii_text = 'H\u00ebll\u00f6 W\u00f6rld !!'\nprint(ascii(ascii_text))\n\n# Multiline String\n\nmultiline_text =''' Hello,\nWelcome to \nPython Tutorials'''\n\nprint(ascii(multiline_text))\n\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'Hello World'\n'H\\xebll\\xf6 W\\xf6rld !!'\n' Hello,\\nWelcome to \\nPython Tutorials'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-ascii-vs-print\">Example 2: ascii() vs print()<\/h2>\n\n\n\n<p>In the below example, we will demonstrate the difference between the <strong><code>ascii()<\/code><\/strong> function vs. the <strong><code>print()<\/code><\/strong> function. The <strong><code>ascii()<\/code><\/strong> function escapes the non-ASCII character, while the <strong><code>print()<\/code><\/strong> function does not escape the value and prints as is.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# Normal string \ntext = 'Hello World'\nprint('ASCII version is ',ascii(text))\nprint('print version is ',text)\n\n# Text with Non-ASCII characters\nascii_text = 'H\u00ebll\u00f6 W\u00f6rld !!'\nprint('ASCII version is ',ascii(ascii_text))\nprint('print version is ',ascii_text)\n\n# Multiline String\n\nmultiline_text =''' Hello,\nWelcome to \nPython Tutorials'''\n\nprint('ASCII version is ',ascii(multiline_text))\nprint('print version is ',multiline_text)\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ASCII version is  'Hello World'\nprint version is  Hello World\nASCII version is  'H\\xebll\\xf6 W\\xf6rld !!'\nprint version is  H\u00ebll\u00f6 W\u00f6rld !!\nASCII version is  ' Hello,\\nWelcome to \\nPython Tutorials'\nprint version is   Hello,\nWelcome to\nPython Tutorials<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The ascii() in Python is a built-in function that returns a printable and readable version of any object such as Strings, Tuples, Lists, etc. The ascii() function will escape the non-ASCII character using&nbsp;\\x, \\u or \\U escapes. ascii() Syntax&nbsp; The syntax of the ascii() method is&nbsp; ascii(object) ascii() Parameters The ascii() function takes an object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":589,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[],"class_list":["post-588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=588"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/588\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/588\/revisions\/590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/589"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}