Python ascii() Function

Python’s built-in ascii(object) function takes one object argument and returns the string representation of that object. The function calls the repr() built-in function and replaces non-ASCII characters with the character code \x. For example, calling ascii('München') results in the ascii string 'M\xfcnchen' by replacing character ü with character code \xfc.

ArgumentobjectIterable such as a list, tuple, or dictionary
Return Valuestring representation with replaced Unicode symbolsCalls the repr(object) function and replaces all non-ASCII characters with their character codes using the backslash notation.
Input : ascii("¥") 
Output : '\xa5' 

Input : ascii("ü") 
Output : '\xfc'

Input : ascii("π") 
Output : '\u03c0'

Input : ascii("µ") 
Output : '\xb5' 

Input : ascii("Ë") 
Output : '\xcb' 

Check out my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Publisher Link: https://nostarch.com/pythononeliners

Example ascii()

The following code shows you how to use the ascii() function on different input arguments.

>>> ascii('München')
"'M\\xfcnchen'"
>>> repr('München')
"'München'"
>>> ascii('New York')
"'New York'"
>>> repr('New York')
"'New York'"
>>> repr([1, 2, 3])
'[1, 2, 3]'
>>> repr(42)
'42'

You can observe multiple properties of the ascii() function:

  • It returns a string representation of any object so it encloses the string in an extra pair of quotes.
  • If the string contains non-ASCII characters, the result is the same as the repr() function.
  • If the string contains only ASCII characters, the result replaces those characters with their character codes using the backslash notation.
  • You can pass integers, lists, sets, dictionaries into the ascii() function.

Summary

Python’s built-in ascii(object) function takes one object argument and returns the string representation of that object.

The function calls the repr() built-in function and replaces non-ASCII characters with the character code \x.

For example, calling ascii('München') results in the ascii string 'M\xfcnchen' by replacing character ü with character code \xfc.

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!