{"id":6901,"date":"2024-04-11T11:58:08","date_gmt":"2024-04-11T11:58:08","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6901"},"modified":"2024-04-11T11:58:08","modified_gmt":"2024-04-11T11:58:08","slug":"how-to-work-with-unicode-in-python","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/how-to-work-with-unicode-in-python\/","title":{"rendered":"How To Work With Unicode In Python?"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#how-to-work-with-unicode-in-python\">How To Work With Unicode In Python?<\/a><\/li><li ><a href=\"#what-is-the-unicode-system\">What is the Unicode system?<\/a><\/li><li ><a href=\"#what-is-character-encoding\">What is Character Encoding?<\/a><\/li><li ><a href=\"#what-is-pythons-unicode-support\">What is Python&#8217;s Unicode Support?<\/a><\/li><li ><a href=\"#example-1\">Example 1<\/a><\/li><li ><a href=\"#example-2\">Example 2<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#how-to-work-with-unicode-in-python-fa-qs\">How To Work With Unicode In Python- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-work-with-unicode-in-python\">How To Work With Unicode In Python?<\/h2>\n\n\n\n<p>Python is an object-oriented programming language. This article will discuss the Unicode system and working with Unicode in Python. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-unicode-system\">What is the Unicode system?<\/h2>\n\n\n\n<p>The Unicode system is a software application that will show output in different languages that are English, French, Japanese, and Hebrew. <a href=\"https:\/\/www.skillvertex.com\/blog\/python-applications\/\" data-type=\"post\" data-id=\"6890\">Python&#8217;<\/a>s string type will use the Unicorn Standard for showing the characters. This way it will allow the program to work with different characters.  <\/p>\n\n\n\n<p>However, a character is considered the smallest possible component of the text. A, B, and C are the different characters. A unicorn string is referred to as a sequence of code points that has a number from 0 through 0x10FFFFFF. The sequence of code will be represented in<a href=\"https:\/\/www.skillvertex.com\/blog\/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc\/\" data-type=\"post\" data-id=\"3096\"> memory <\/a>as code unit sets and code units are then converted into 8-bit bytes<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-character-encoding\">What is Character Encoding?<\/h2>\n\n\n\n<p>A sequence of code will be represented in<a href=\"https:\/\/www.skillvertex.com\/blog\/what-is-a-memory-leak-how-can-we-avoid-it\/\" data-type=\"post\" data-id=\"3174\"> memory<\/a> in the form of code units. Therefore, the rule for converting a Unicode string to a sequence of bytes is called character encoding.<\/p>\n\n\n\n<p>There are three types of encoding such as UTF-8, UTF-16 and UTF-32. The full form of UTF is the Unicode Transformation Format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-pythons-unicode-support\">What is Python&#8217;s Unicode Support?<\/h2>\n\n\n\n<p>Python 3.0 has built-in support for Unicode. The str type has Unicode characters and has strings such as single, double, or triple-quoted string syntax which is stored as Unicode. The default encoding for the Python source code is UTF-8.<\/p>\n\n\n\n<p>The string has a literal representation of the Unicode character.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var = \"3\/4\"\nprint (var)\nvar = \"\\u00BE\"\nprint (var)<\/code><\/pre>\n\n\n\n<p>The code has an output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3\/4\n\u00be<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1\">Example 1<\/h2>\n\n\n\n<p>In the example provided below has a string 10  which will be stored with the Unicode values of 1 and 0 and has the values \\u0031 and u0030 respectively<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var = \"\\u0031\\u0030\"\nprint (var)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<p>The string will show the text in a format that is human-readable. The bytes will store characters as binary data. Whereas, encoding will translate data from a character string into a series of bytes. Decoding refers to a process where the bytes back will translate into a human-readable character.<\/p>\n\n\n\n<p>In the example given below, it has a string variable that consists of ASCII characters. ASCII is a sub-division of a Unicode character set. The encoding method will use utf-8. The decode method will translate back to str object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello\"\ntobytes = string.encode('utf-8')\nprint (tobytes)\nstring = tobytes.decode('utf-8')\nprint (string)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>b'Hello'\nHello<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2\">Example 2<\/h2>\n\n\n\n<p>The rupee symbol will be stored in the variable with the Unicorn value. Then, translate the string into bytes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"\\u20B9\"\nprint (string)\ntobytes = string.encode('utf-8')\nprint (tobytes)\nstring = tobytes.decode('utf-8')\nprint (string)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u20b9\nb'\\xe2\\x82\\xb9'\n\u20b9<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, this article is about the working of  Unicode in Python. Several examples are illustrated in this article to understand it more clearly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-work-with-unicode-in-python-fa-qs\">How To Work With Unicode In Python- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1706703545039\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How do I print a Unicode character in a string in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Print Unicode Character with the ord() Function.  We can print Unicode character through <strong>combining ord() with the chr() function<\/strong> .<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1706703552327\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.  How do you write Unicode in a string?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. You can add a special character to a string using its unique code. There are three ways to do this. Special characters such as <strong>\\xXX<\/strong>,<strong>\\uXXXX<\/strong> and<br \/><strong>\\u{X\u2026}<\/strong><\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1706703559832\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the Unicode format for Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. \u00a0Unicode is referred to as the mapping, and UTF-8 enables a computer to understand that mapping. In Python 3, the default string encoding is UTF-8, which means that the Unicode code point in the Python string is automatically converted into the corresponding character.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>How To Work With Unicode In Python? Python is an object-oriented programming language. This article will discuss the Unicode system and working with Unicode in Python. What is the Unicode system? The Unicode system is a software application that will show output in different languages that are English, French, Japanese, and Hebrew. Python&#8217;s string type &#8230; <a title=\"How To Work With Unicode In Python?\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/how-to-work-with-unicode-in-python\/\" aria-label=\"More on How To Work With Unicode In Python?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6902,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[72,57],"class_list":["post-6901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-programming","tag-python","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=6901"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6901\/revisions"}],"predecessor-version":[{"id":8891,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6901\/revisions\/8891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6902"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}