isascii, isspace, isprintable & isidentifier in Python
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

isascii, isspace, isprintable & isidentifier in Python

Publish Date: Aug 22
0 0

Buy Me a Coffee

*Memos for string and byte string(bytes and bytearray) functions:

*Memos for string and byte string(bytes and bytearray):

str.isascii() and bytes.isascii() or bytearray.isascii() can check if the string and byte string respectively only have one or more ASCII characters and bytes and are empty as shown below. *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

<Byte String(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 byte string respectively only have one or more ASCII whitespace characters and bytes and aren't empty as shown below:

*Memos:

  • 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

<Byte String(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:

*Memos:

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

<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:

*Memos:

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

<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

Comments 0 total

    Add comment