BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python ascii() Function with examples

By Chaitanya Singh | Filed Under: Python Tutorial

The ascii() Function in Python returns readable representation of an object (String, tuple, list, dictionary etc.). In this tutorial we will see the usage of ascii function with the help of examples.

Python ascii() function example

In the following example we have two strings myString and myText. The first string contains a non-ascii character and the second string is just a plain text with no non-ascii characters. As you can see in the output that the non-ascii character is replaced with a readable representation of the non-ascii character.

The non-ascii characters in a string are escaped using \x, \u or \U, as shown in the following example (the third print statement).

# A string containing non-ascii character
myString = 'Ä Book'
print(ascii(myString))

# A normal string
myText = "This is a plain text"
print(ascii(myText))

# Displaying a string using print that
#  contains ascii value
print('\xc5 Book')

Output:
Python ascii function example

Python ascii function example with list and tuple

# tuple
t = ('A', 'B', 'Ä')
print(ascii(t))

# list
lis = ['BeginnersBook', 'BeginnersBöök']
print(ascii(lis))

Output:
Python ascii function tuple list example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap