21

Is there a field or a function that would return all ASCII characters in python's standard library?

bzrr
  • 1,490
  • 3
  • 20
  • 39

4 Answers4

30

You can make one.

ASCII = ''.join(chr(x) for x in range(128))

If you need to check for membership, there are other ways to do it:

if c in ASCII:
    # c is an ASCII character

if c <= '\x7f':
    # c is an ASCII character

If you want to check that an entire string is ASCII:

def is_ascii(s):
    """Returns True if a string is ASCII, False otherwise."""
    try:
        s.encode('ASCII')
        return True
    except UnicodeEncodeError:
        return False
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • I was actually looking for this very answer, but note that it will not be very obvious what you are doing here. – Roy Prins Aug 09 '17 at 20:13
20

You can use the string module:

import string
print string.printable

which gives:

'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
jh314
  • 27,144
  • 16
  • 62
  • 82
  • 9
    I can't say for sure that this isn't what they were looking for, but ASCII defines several non-printable characters, too. – icktoofay Jul 29 '13 at 04:19
  • This is the only answer that answers the question, technically. string.printable is a variable (or for the purposes of the question, effectively such, if it isn't literally). – Alex Hall Jul 13 '16 at 22:51
3

I don't know of any included python module that has such a attribute. However, the easiest and shortest way is probably just to create it yourself

standard_ascii = [chr(i) for i in xrange(128)]

or

extended_ascii = [chr(i) for i in xrange(256)]

for the extended ascii character list.

Note that

import string
string.printable

does not include all of the 127 standard ascii characters, which you can see by

len(string.printable)
> 100

If you want them as string instead of a list, just add an "".join(), like so:

extended_ascii = "".join([chr(i) for i in xrange(256)])
  • What was maybe formerly called "extended ASCII" is nowadays one of many possible character sets which all are (mostly) mutually exclusive. So there really is no such thing as "extended ASCII". – glglgl Jul 29 '13 at 12:54
  • Then let's maybe talk about 7-bit and 8-bit ASCII to be more precise –  Jul 29 '13 at 15:31
  • 1
    I'm not sure the "extended ASCII" works using the method above - for example, the first character in extended ascii (Windows-1252) is the Euro character €, but the first character generated by `chr(128)` is `\0x80` which in Unicode is some weird `pad` character. You get a € with `chr(8364)`. The perceptual problem is that `chr()` is keyed to ascii in other languages, but is keyed to Unicode in Python. N.B. I only realise this after coding something very similar to the above, and later discovering it's failing to match certain characters I'd have expected it to find. – Thomas Kimber Dec 04 '18 at 11:00
2

You could use the Python Standard Library module curses.ascii. Some of the included functions include:

curses.ascii.isascii() # Checks for a character value in the 7-bit ASCII set.
curses.ascii.iscntrl() # Checks for an ASCII control character (in the range 0x00 to 0x1f).
curses.ascii.isalpha() # Check for an ASCII alphabetic character.

From the documentation:

The curses.ascii module supplies name constants for ASCII characters and functions to test membership in various ASCII character classes.

Note that the curses module is may not be available on a Windows system:

The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling.

While curses is most widely used in the Unix environment, versions are available for DOS, OS/2, and possibly other systems as well. This extension module is designed to match the API of ncurses, an open-source curses library hosted on Linux and the BSD variants of Unix.

Community
  • 1
  • 1
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163