Skip to main content

Command Palette

Search for a command to run...

ascii() function in Python

Updated
1 min readView as Markdown
ascii() function in Python
A
Software Engineer at OpenText passionate about building scalable web applications and backend systems. I work mainly with Java, Spring Boot, Python, and modern web technologies. I enjoy creating things for the internet, exploring new tech, and sharing what I learn along the way.

Introduction

The ascii() function returns a string containing a printable representation of an object, escaping the non-ASCII characters in the string using \x, \u, or \U escapes.

The syntax of ascii() function looks like this:

ascii(object)

where object can be String, List, Tuple, Dictionary, etc.

Examples

message = "I love iRead"
print(ascii(message))

Output:

'I love iRead'

With ASCII characters, the ascii() function doesn't make much difference.

message = "I love îReâd"
print(ascii(message))

Output:

'I love \xeeRe\xe2d'

But when non-ASCII characters are added, the ascii() function escapes them using \x, \u, or \U. For example, îis replaced by\xee. and â is replaced by \xe2.

If you print the above output as below:

print("I love \xeeRe\xe2d")

You will get this as output:

I love îReâd

which is obvious.

Conclusion

In this part, we learned about the Python ascii() function with the help of examples.

More from this blog

A

Ashutosh Writes

113 posts

Ashutosh Writes is a tech-focused blog where practical learning connects with real-world development. It features clear, engaging articles on web development, Python, Java, artificial intelligence, and modern software engineering. From hands-on project tutorials and coding guides to AI concepts and development insights, the blog is designed to simplify complex topics and help developers learn, build, and grow at every stage of their journey.