{"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>
\nascii() Function in Python:<\/strong><\/p>\n

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

\u00e5<\/code> has been replaced with \\xe5.<\/code><\/p>\n

Syntax:<\/strong><\/p>\n

ascii(object)<\/pre>\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

Given first string = \"welcome to Pyth\u00f6n-Pr\u00f6grams\"\r\nGiven second string = \"hello this is btechgeeks\"<\/pre>\n

Output:<\/strong><\/p>\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

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given first string =  \"g\u00f6\u00f6d morning all\"\r\nGiven second string = \"hello this is vikr\u00e5m chiluka\"<\/pre>\n

Output:<\/strong><\/p>\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