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' 

But before we move on, I’m excited to present you 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).

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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!