{"id":24556,"date":"2021-10-21T10:14:17","date_gmt":"2021-10-21T04:44:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=24556"},"modified":"2021-11-05T19:37:12","modified_gmt":"2021-11-05T14:07:12","slug":"python-ascii-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-ascii-method-with-examples\/","title":{"rendered":"Python ascii() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for all() Function<\/a> The ascii() function returns an object’s readable version (Strings, Tuples, Lists, etc).<\/p>\n Any non-ascii characters will be replaced with escape characters by the ascii() function.<\/p>\n It uses the \\x, \\u, or \\U escapes to escape non-ASCII characters in the string.<\/p>\n For Example:<\/p>\n \u00f6 has been replaced with \\xf6n.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n object:<\/strong> An object, such as a String, List, Tuple, Dictionary, and so on.<\/p>\n Return Value:<\/strong><\/p>\n It returns a string containing an object’s printable representation.<\/p>\n For instance, \u00f6<\/strong> is changed to \\xf6n and \u221a<\/strong> is changed to \\u221a.<\/p>\n The string’s non-ASCII characters are escaped with \\x, \\u, or \\U.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for all() Function ascii() Function in Python: The ascii() function returns an object’s readable version (Strings, Tuples, Lists, etc). Any non-ascii characters will be replaced with escape characters by the ascii() function. It uses the \\x, \\u, or \\U escapes to escape non-ASCII characters in the …<\/p>\n
\nascii() Function in Python:<\/strong><\/p>\n\u00e5<\/code> has been replaced with
\\xe5.<\/code><\/p>\n
ascii(object)<\/pre>\n
Given first string = \"welcome to Pyth\u00f6n-Pr\u00f6grams\"\r\nGiven second string = \"hello this is btechgeeks\"<\/pre>\n
The given first string after applying ascii() function = 'welcome to Pyth\\xf6n-Pr\\xf6grams'\r\nThe given second string after applying ascii() function = 'hello this is btechgeeks'<\/pre>\n
Given first string = \"g\u00f6\u00f6d morning all\"\r\nGiven second string = \"hello this is vikr\u00e5m chiluka\"<\/pre>\n
The given first string after applying ascii() function = 'g\\xf6\\xf6d morning all'\r\nThe given second string after applying ascii() function = 'hello this is vikr\\xe5m chiluka'<\/pre>\n
Program for ascii() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first string as static input and store it in a variable.\r\ngvn_str = \"welcome to Pyth\u00f6n-Pr\u00f6grams\"\r\n# Apply ascii() function on the given string that returns an object's\r\n# readable version.Any non-ascii characters will be replaced with escape\r\n# characters by the ascii() function.\r\n# Store it in another variable.\r\nrslt_str1 = ascii(gvn_str)\r\n# Print the string after applying ascii() function on the given first string .\r\nprint(\"The given first string after applying ascii() function = \", rslt_str1)\r\n# similarly do the same for the other string and print the result string.\r\ngvn_str2 = \"hello this is btechgeeks\"\r\nrslt_str2 = ascii(gvn_str2)\r\nprint(\"The given second string after applying ascii() function = \", rslt_str2)\r\n<\/pre>\n
The given first string after applying ascii() function = 'welcome to Pyth\\xf6n-Pr\\xf6grams'\r\nThe given second string after applying ascii() function = 'hello this is btechgeeks'<\/pre>\n
For List<\/h3>\n
gvn_lst = [\"pyth\u00f6n\", \"vikr\u00e5m\", \"welcome\"]\r\nrslt_lst = ascii(gvn_lst)\r\nprint(rslt_lst)\r\n<\/pre>\n
['pyth\\xf6n', 'vikr\\xe5m', 'welcome']<\/pre>\n
\n