Python ascii() Method with Examples

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 string.

For Example:

ö has been replaced with \xf6n.

Ã¥ has been replaced with \xe5.

Syntax:

ascii(object)

Parameters

object: An object, such as a String, List, Tuple, Dictionary, and so on.

Return Value:

It returns a string containing an object’s printable representation.

For instance, ö is changed to \xf6n and √ is changed to \u221a.

The string’s non-ASCII characters are escaped with \x, \u, or \U.

Examples:

Example1:

Input:

Given first string = "welcome to Pythön-Prögrams"
Given second string = "hello this is btechgeeks"

Output:

The given first string after applying ascii() function =  'welcome to Pyth\xf6n-Pr\xf6grams'
The given second string after applying ascii() function =  'hello this is btechgeeks'

Example2:

Input:

Given first string =  "gööd morning all"
Given second string = "hello this is vikråm chiluka"

Output:

The given first string after applying ascii() function =  'g\xf6\xf6d morning all'
The given second string after applying ascii() function =  'hello this is vikr\xe5m chiluka'

Program for ascii() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Apply ascii() function on the given string that returns an object’s readable version. Any non-ascii characters will be replaced with escape characters by the ascii() function.
  • Store it in another variable.
  • Print the string after applying ascii() function on the given first string .
  • Similarly, do the same for the other string and print the result string.
  • The Exit of the Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_str = "welcome to Pythön-Prögrams"
# Apply ascii() function on the given string that returns an object's
# readable version.Any non-ascii characters will be replaced with escape
# characters by the ascii() function.
# Store it in another variable.
rslt_str1 = ascii(gvn_str)
# Print the string after applying ascii() function on the given first string .
print("The given first string after applying ascii() function = ", rslt_str1)
# similarly do the same for the other string and print the result string.
gvn_str2 = "hello this is btechgeeks"
rslt_str2 = ascii(gvn_str2)
print("The given second string after applying ascii() function = ", rslt_str2)

Output:

The given first string after applying ascii() function =  'welcome to Pyth\xf6n-Pr\xf6grams'
The given second string after applying ascii() function =  'hello this is btechgeeks'

For List

Example:

gvn_lst = ["pythön", "vikråm", "welcome"]
rslt_lst = ascii(gvn_lst)
print(rslt_lst)

Output:

['pyth\xf6n', 'vikr\xe5m', 'welcome']

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.