Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Python ascii() Method

Python ascii() method is language inbuilt function and returns a string containing a printable representation of an object. ascii() escapes the non-ASCII characters in the string using \x, \u or \U escapes. 1. Syntax The syntax of the ascii() method is: Here the object is required method argument …

Lokesh Gupta

October 2, 2022

Python Built-in Functions
Python Basics
Python

Python ascii() method is language inbuilt function and returns a string containing a printable representation of an object. ascii() escapes the non-ASCII characters in the string using \x, \u or \U escapes.

1. Syntax

The syntax of the ascii() method is:

ascii(object)

Here the object is required method argument and it can be any Python object such as String, List, Tuple, Dictionary.

The return value is a string containing the printable characters only. All non-printable characters are escaped using \x, \u or \U and will use the shortest available escape sequence between all three escapes.

For example, å will be replaced with \xe5.

2. Python ascii() example

A simple python program which prints a nornal string, and then another string which contains special characters. We will also learn to use ascii() with List object.

simpleText = "Python rocks"
print(ascii(simpleText))

blogName = "HowToDoInJåvå"
print(ascii(blogName))

nameList = ['how', 'to', 'do', 'in', 'jåvå']
print(ascii(nameList))

Program output.

'Python rocks'

'HowToDoInJ\xe5v\xe5'

['how', 'to', 'do', 'in', 'j\xe5v\xe5']

3. Difference between ascii() and ord()

Python also has ord() function which returns a numeric ASCII value correcsponding to the given character or symbol. For example, the ASCII value of the letter ‘B’ is 66. Refer the ascii table for all supported characters.

The noticeable difference between both methods is that ascii() method works for objects, the whole ord() method works for any given single character.

text = "jåvå"
print(ascii(text))

char = 'A'
print(char)

char = 'å'
print(char)

Program output.

'j\xe5v\xe5'
A
å

Happy Learning !!

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python Examples

Python Built-in Functions

  • Python abs()
  • Python all()
  • Python any()
  • Python ascii()
  • Python bin()
  • Python input()
  • Python range()
  • Python print()

Python String Functions

  • String capitalize()
  • String count()
  • String endswith()
  • String split()
  • String startswith()
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Python Hello World Program

Next

Python bin() Method

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap