DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

isascii, isspace, isprintable & isidentifier in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.isascii() and bytes.isascii() or bytearray.isascii() can check if the string and bytes or bytearray respectively only have one or more ASCII characters and bytes and are empty as shown below:

*Memo:

  • Each has no arguments.

<String>:

print('John Smith'.isascii()) print('Python3'.isascii()) print(''.isascii()) # True  print('Jφhи Sмiтh'.isascii()) # False 
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

print(b'John Smith'.isascii()) print(bytearray(b'John Smith').isascii()) print(b'Python3'.isascii()) print(bytearray(b'Python3').isascii()) print(b''.isascii()) print(bytearray(b'').isascii()) # True  print("Lёт's gφ!".encode().isascii()) print(b"L\xd1\x91\xd1\x82's g\xcf\x86!".isascii()) print(bytearray(b"L\xd1\x91\xd1\x82's g\xcf\x86!").isascii()) # False 
Enter fullscreen mode Exit fullscreen mode

str.isspace() and bytes.isspace() or bytearray.isspace() can check if the string and bytes or bytearray respectively only have one or more ASCII whitespace characters and bytes and aren't empty as shown below:

*Memo:

  • Each has no arguments.
  • My post explains line boundaries.

<String>:

print(' '.isspace()) print(' '.isspace()) print('\t\n\r\v\x0b\f\x0c'.isspace()) print('\x1c\x1d\x1e\x85\u2028\u2029'.isspace()) # True  print('Python 3'.isspace()) print(''.isspace()) # False 
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

print(b' '.isspace()) print(bytearray(b' ').isspace()) print(b' '.isspace()) print(bytearray(b' ').isspace()) # True  print('\t\n\r\v\x0b\f\x0c'.encode().isspace()) print(b'\t\n\r\x0b\x0b\x0c\x0c'.isspace()) print(bytearray(b'\t\n\r\x0b\x0b\x0c\x0c').isspace()) # True  print('\x1c\x1d\x1e\x85\u2028\u2029'.encode().isspace()) print(b'\x1c\x1d\x1e\xc2\x85\xe2\x80\xa8\xe2\x80\xa9'.isspace()) print(bytearray(b'\x1c\x1d\x1e\xc2\x85\xe2\x80\xa8\xe2\x80\xa9').isspace()) # False  print(b'Python 3'.isspace()) print(bytearray(b'Python 3').isspace()) print(b''.isspace()) print(bytearray(b'').isspace()) # False 
Enter fullscreen mode Exit fullscreen mode

str.isprintable() can check if the string only has one or more printable characters and is empty as shown below:

*Memo:

  • It has no arguments.
  • bytes.isprintable() and bytearray.isprintable() don't exist for a bytes and bytearray respectively.

<String>:

print('Hello World'.isprintable()) print('I\'m John'.isprintable()) print(''.isprintable()) # True  print('Hello\tWorld'.isprintable()) print('Hello\nWorld'.isprintable()) # False  print('I\'m John') # I'm John  print('Hello\tWorld') # Hello World  print('Hello\nWorld') # Hello # World 
Enter fullscreen mode Exit fullscreen mode

str.isidentifier() can check if the string is a valid identifier in Python according to Identifiers and keywords and isn't empty as shown below:

*Memo:

  • It has no arguments.
  • bytes.isidentifier() and bytearray.isidentifier() don't exist for a bytes and bytearray respectively.

<String>:

print('True_100'.isidentifier()) print('tRuE_100'.isidentifier()) print('_True100'.isidentifier()) print('True100_'.isidentifier()) print('True'.isidentifier()) print('class'.isidentifier()) print('def'.isidentifier()) # True  print('True-100'.isidentifier()) print('100_True'.isidentifier()) print(''.isidentifier()) # False 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)