*Memos for string and byte string(bytes
and bytearray
) functions:
- My post explains isdecimal(), isdigit() and isnumeric().
- My post explains isalpha() and isalnum().
- My post explains iskeyword() and issoftkeyword().
- My post explains encode() and decode().
*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
<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
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
<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
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()
andbytearray.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
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()
andbytearray.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