assert 'HELLO'.lower() == 'hello'
assert 'Hello World'.lower() == 'hello world'
assert 'hello'.lower() == 'hello'
assert ''.lower() == ''
assert '123'.lower() == '123'
assert 'hello'.upper() == 'HELLO'
assert 'Hello World'.upper() == 'HELLO WORLD'
assert 'HELLO'.upper() == 'HELLO'
assert ''.upper() == ''
assert '123'.upper() == '123'
assert 'hello'.capitalize() == 'Hello'
assert 'HELLO'.capitalize() == 'Hello'
assert 'hELLO wORLD'.capitalize() == 'Hello world'
assert ''.capitalize() == ''
assert '123abc'.capitalize() == '123abc'
assert 'hello world'.title() == 'Hello World'
assert 'HELLO WORLD'.title() == 'Hello World'
assert "they're".title() == "They'Re"
assert ''.title() == ''
assert '123 abc'.title() == '123 Abc'
assert 'Hello World'.swapcase() == 'hELLO wORLD'
assert 'HELLO'.swapcase() == 'hello'
assert 'hello'.swapcase() == 'HELLO'
assert ''.swapcase() == ''
assert 'Hello'.casefold() == 'hello'
assert 'HELLO'.casefold() == 'hello'
assert ''.casefold() == ''
assert 'hello'.isalpha() == True
assert 'Hello'.isalpha() == True
assert ''.isalpha() == False
assert 'hello123'.isalpha() == False
assert 'hello world'.isalpha() == False
assert '123'.isdigit() == True
assert ''.isdigit() == False
assert '123abc'.isdigit() == False
assert '12 34'.isdigit() == False
assert 'hello123'.isalnum() == True
assert 'hello'.isalnum() == True
assert '123'.isalnum() == True
assert ''.isalnum() == False
assert 'hello 123'.isalnum() == False
assert '123'.isnumeric() == True
assert ''.isnumeric() == False
assert '123abc'.isnumeric() == False
assert ' '.isspace() == True
assert '\t\n'.isspace() == True
assert ''.isspace() == False
assert ' a '.isspace() == False
assert 'hello'.islower() == True
assert 'Hello'.islower() == False
assert ''.islower() == False
assert '123'.islower() == False
assert 'hello123'.islower() == True
assert 'HELLO'.isupper() == True
assert 'Hello'.isupper() == False
assert ''.isupper() == False
assert '123'.isupper() == False
assert 'HELLO123'.isupper() == True
assert 'hello'.isascii() == True
assert ''.isascii() == True
assert '\x00\x7f'.isascii() == True
assert '123'.isdecimal() == True
assert ''.isdecimal() == False
assert '123abc'.isdecimal() == False
assert 'hello'.find('l') == 2
assert 'hello'.find('ll') == 2
assert 'hello'.find('x') == -1
assert 'hello'.find('') == 0
assert 'hello'.find('l', 3) == 3
assert 'hello'.find('l', 0, 3) == 2
_I64_MIN = -(2**63)
assert 'hello'.find('h', _I64_MIN) == 0
assert 'hello'.find('h', 0, _I64_MIN) == -1
assert 'hello'.startswith('h', _I64_MIN) == True
assert 'hello'.startswith('h', _I64_MIN, _I64_MIN) == False
assert 'hello'.rfind('l') == 3
assert 'hello'.rfind('x') == -1
assert 'hello'.rfind('l', 0, 3) == 2
assert 'hello'.index('l') == 2
assert 'hello'.index('ll') == 2
assert 'hello'.rindex('l') == 3
assert 'hello'.count('l') == 2
assert 'hello'.count('ll') == 1
assert 'hello'.count('x') == 0
assert 'hello'.count('') == 6
assert 'aaa'.count('a') == 3
assert 'hello'.startswith('he') == True
assert 'hello'.startswith('lo') == False
assert 'hello'.startswith('') == True
assert 'hello'.startswith('ell', 1) == True
assert 'hello'.endswith('lo') == True
assert 'hello'.endswith('he') == False
assert 'hello'.endswith('') == True
assert 'hello'.endswith('ell', 0, 4) == True
assert ' hello '.strip() == 'hello'
assert 'xxhelloxx'.strip('x') == 'hello'
assert 'hello'.strip() == 'hello'
assert ''.strip() == ''
assert ' '.strip() == ''
assert ' hello '.lstrip() == 'hello '
assert 'xxhello'.lstrip('x') == 'hello'
assert 'hello'.lstrip() == 'hello'
assert ' hello '.rstrip() == ' hello'
assert 'helloxx'.rstrip('x') == 'hello'
assert 'hello'.rstrip() == 'hello'
assert 'hello world'.removeprefix('hello ') == 'world'
assert 'hello world'.removeprefix('world') == 'hello world'
assert 'hello'.removeprefix('') == 'hello'
assert 'hello world'.removesuffix(' world') == 'hello'
assert 'hello world'.removesuffix('hello') == 'hello world'
assert 'hello'.removesuffix('') == 'hello'
assert 'a b c'.split() == ['a', 'b', 'c']
assert 'a,b,c'.split(',') == ['a', 'b', 'c']
assert 'a,b,c'.split(',', 1) == ['a', 'b,c']
assert ' a b '.split() == ['a', 'b']
assert 'hello'.split('x') == ['hello']
assert 'a b c'.rsplit() == ['a', 'b', 'c']
assert 'a,b,c'.rsplit(',') == ['a', 'b', 'c']
assert 'a,b,c'.rsplit(',', 1) == ['a,b', 'c']
assert 'hello world'.rsplit(maxsplit=1) == ['hello', 'world']
assert 'a b c'.rsplit(maxsplit=1) == ['a b', 'c']
assert 'a b c'.rsplit(maxsplit=2) == ['a', 'b', 'c']
assert 'a b c'.rsplit(maxsplit=0) == ['a b c']
assert 'a b'.rsplit(maxsplit=2) == ['a', 'b']
assert 'a\xa0\xa0b'.rsplit(maxsplit=2) == ['a', 'b']
assert ' a b '.rsplit(maxsplit=1) == [' a', 'b']
assert 'a\nb\nc'.splitlines() == ['a', 'b', 'c']
assert 'a\nb\nc'.splitlines(True) == ['a\n', 'b\n', 'c']
assert 'a\r\nb'.splitlines() == ['a', 'b']
assert ''.splitlines() == []
assert 'hello world'.partition(' ') == ('hello', ' ', 'world')
assert 'hello'.partition('x') == ('hello', '', '')
assert 'hello world test'.partition(' ') == ('hello', ' ', 'world test')
assert 'hello world'.rpartition(' ') == ('hello', ' ', 'world')
assert 'hello'.rpartition('x') == ('', '', 'hello')
assert 'hello world test'.rpartition(' ') == ('hello world', ' ', 'test')
assert 'hello'.replace('l', 'L') == 'heLLo'
assert 'hello'.replace('l', 'L', 1) == 'heLlo'
assert 'hello'.replace('x', 'y') == 'hello'
assert 'aaa'.replace('a', 'b') == 'bbb'
assert ''.replace('a', 'b') == ''
assert 'hi'.center(6) == ' hi '
assert 'hi'.center(6, '-') == '--hi--'
assert 'hi'.center(2) == 'hi'
assert 'hi'.center(1) == 'hi'
assert 'hi'.ljust(6) == 'hi '
assert 'hi'.ljust(6, '-') == 'hi----'
assert 'hi'.ljust(2) == 'hi'
assert 'hi'.rjust(6) == ' hi'
assert 'hi'.rjust(6, '-') == '----hi'
assert 'hi'.rjust(2) == 'hi'
assert '42'.zfill(5) == '00042'
assert '-42'.zfill(5) == '-0042'
assert '+42'.zfill(5) == '+0042'
assert '42'.zfill(2) == '42'
assert ''.zfill(3) == '000'
assert 'hello'.startswith(('he', 'lo')) == True
assert 'hello'.startswith(('lo', 'he')) == True
assert 'hello'.startswith(('x', 'y')) == False
assert 'hello'.endswith(('he', 'lo')) == True
assert 'hello'.endswith(('lo', 'he')) == True
assert 'hello'.endswith(('x', 'y')) == False
assert 'hello'.startswith(('ell',), 1) == True
assert 'hello'.startswith(('he', 1)) == True
assert 'hello'.endswith(('lo', 1)) == True
try:
'hello'.startswith(('xx', 1))
assert False, 'expected startswith invalid tuple element to raise'
except TypeError as exc:
assert str(exc) == 'tuple for startswith must only contain str, not int'
try:
'hello'.endswith((1, 'lo'))
assert False, 'expected endswith invalid element before match to raise'
except TypeError as exc:
assert str(exc) == 'tuple for endswith must only contain str, not int'
try:
'hello'.startswith(42)
assert False, 'expected startswith non-str affix to raise'
except TypeError as exc:
assert str(exc) == 'startswith first arg must be str or a tuple of str, not int'
try:
'hello'.endswith(None)
assert False, 'expected endswith None affix to raise'
except TypeError as exc:
assert str(exc) == 'endswith first arg must be str or a tuple of str, not NoneType'
try:
'hello'.startswith(42, 'x')
assert False, 'expected startswith bad start to raise'
except TypeError as exc:
assert str(exc) == 'slice indices must be integers or None or have an __index__ method'
try:
'hello'.endswith(('lo', 1), None, 'x')
assert False, 'expected endswith bad end to raise'
except TypeError as exc:
assert str(exc) == 'slice indices must be integers or None or have an __index__ method'
assert 'hello'.startswith('ello', True) == True
assert 'hello'.count('l', True) == 2
assert 'hello'.find('l', None) == 2
assert 'hello'.find('l', None, None) == 2
assert 'hello'.find('l', 0, None) == 2
assert 'hello'.rfind('l', None, None) == 3
assert 'hello'.count('l', None, None) == 2
assert 'hello'.startswith('he', None) == True
assert 'hello'.endswith('lo', None, None) == True
assert ' hello '.strip(None) == 'hello'
assert ' hello '.lstrip(None) == 'hello '
assert ' hello '.rstrip(None) == ' hello'
assert 'a,b,c'.split(sep=',') == ['a', 'b', 'c']
assert 'a,b,c'.split(',', maxsplit=1) == ['a', 'b,c']
assert 'a,b,c'.split(sep=',', maxsplit=1) == ['a', 'b,c']
assert 'a,b,c'.rsplit(sep=',') == ['a', 'b', 'c']
assert 'a,b,c'.rsplit(',', maxsplit=1) == ['a,b', 'c']
assert 'a,b,c'.rsplit(sep=',', maxsplit=1) == ['a,b', 'c']
assert 'a\nb\nc'.splitlines(keepends=True) == ['a\n', 'b\n', 'c']
assert 'a\nb\nc'.splitlines(keepends=False) == ['a', 'b', 'c']
assert 'aaa'.replace('a', 'b', count=2) == 'bba'
assert 'hello'.encode() == b'hello'
assert 'hello'.encode('utf-8') == b'hello'
assert 'hello'.encode('utf8') == b'hello'
assert 'hello'.encode('utf_8') == b'hello'
assert 'hello'.encode('UTF-8') == b'hello'
assert ''.encode() == b''
assert 'hello'.encode('utf-8', 'strict') == b'hello'
assert 'hello'.encode('ascii') == b'hello'
assert 'hello'.encode('us-ascii') == b'hello'
assert 'hello'.encode('us_ascii') == b'hello'
assert 'hello'.encode('US_ASCII') == b'hello'
assert 'hello'.encode('ASCII') == b'hello'
assert 'héllo wörld ⚡'.encode('ascii', 'ignore') == b'hllo wrld '
assert 'héllo wörld ⚡'.encode('ascii', 'replace') == b'h?llo w?rld ?'
assert 'héllo wörld ⚡'.encode('ascii', 'backslashreplace') == b'h\\xe9llo w\\xf6rld \\u26a1'
assert 'a\U0001f600b'.encode('ascii', 'backslashreplace') == b'a\\U0001f600b'
assert 'café — 日本語 test'.encode('ascii', 'ignore').decode('ascii') == 'caf test'
try:
'héllo'.encode('ascii')
assert False, 'encode ascii of non-ascii string should error'
except ValueError as e:
assert isinstance(e, UnicodeEncodeError)
assert type(e).__name__ == 'UnicodeEncodeError', f'exception type name: {type(e).__name__}'
assert str(e) == "'ascii' codec can't encode character '\\xe9' in position 1: ordinal not in range(128)", (
f'encode ascii strict single-char message: {e}'
)
try:
'aéöbé'.encode('ascii')
assert False, 'encode ascii of non-ascii string should error'
except UnicodeEncodeError as e:
assert str(e) == "'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)", (
f'encode ascii strict multi-char range message: {e}'
)
try:
'éxyz'.encode('ascii')
assert False, 'encode ascii of non-ascii string should error'
except UnicodeEncodeError as e:
assert str(e) == "'ascii' codec can't encode character '\\xe9' in position 0: ordinal not in range(128)", (
f'encode ascii strict bad char at position 0: {e}'
)
try:
'xyzé'.encode('ascii')
assert False, 'encode ascii of non-ascii string should error'
except UnicodeEncodeError as e:
assert str(e) == "'ascii' codec can't encode character '\\xe9' in position 3: ordinal not in range(128)", (
f'encode ascii strict bad char at last position: {e}'
)
try:
'é'.encode('ascii')
assert False, 'encode ascii of non-ascii string should error'
except UnicodeEncodeError as e:
assert str(e) == "'ascii' codec can't encode character '\\xe9' in position 0: ordinal not in range(128)", (
f'encode ascii strict single-char string: {e}'
)
assert 'héllo ⚡'.encode('ascii', 'xmlcharrefreplace') == b'héllo ⚡'
assert 'a\U0001f600b'.encode('ascii', 'xmlcharrefreplace') == b'a😀b'
assert 'héllo ⚡'.encode('ascii', 'namereplace') == b'h\\N{LATIN SMALL LETTER E WITH ACUTE}llo \\N{HIGH VOLTAGE SIGN}'
assert '一'.encode('ascii', 'namereplace') == b'\\N{CJK UNIFIED IDEOGRAPH-4E00}'
assert 'a\x80\x9fb'.encode('ascii', 'namereplace') == b'a\\x80\\x9fb'
assert 'hello'.encode('ascii', 'surrogateescape') == b'hello'
assert 'hello'.encode('ascii', 'surrogatepass') == b'hello'
try:
'héllo'.encode('ascii', 'surrogateescape')
assert False, 'encode ascii surrogateescape of non-ascii string should error'
except UnicodeEncodeError as e:
assert str(e) == "'ascii' codec can't encode character '\\xe9' in position 1: ordinal not in range(128)", (
f'encode ascii surrogateescape behaves like strict: {e}'
)
try:
'héllo'.encode('ascii', 'surrogatepass')
assert False, 'encode ascii surrogatepass of non-ascii string should error'
except UnicodeEncodeError as e:
assert str(e) == "'ascii' codec can't encode character '\\xe9' in position 1: ordinal not in range(128)", (
f'encode ascii surrogatepass behaves like strict: {e}'
)
assert 'hello'.encode('ascii', 'bogus') == b'hello'
try:
'héllo'.encode('ascii', 'bogus')
assert False, 'encode ascii with unknown error handler should error'
except LookupError as e:
assert str(e) == "unknown error handler name 'bogus'", f'encode ascii unknown error handler: {e}'
try:
'hello'.encode('not-a-real-codec')
assert False, 'encode with unsupported codec should error'
except LookupError as e:
assert str(e) == 'unknown encoding: not-a-real-codec', f'encode unknown encoding: {e}'
for bad, expected_type in ((42, 'int'), (None, 'None'), (b'utf-8', 'bytes')):
try:
'hello'.encode(bad)
assert False, f'encode({bad!r}) should error'
except TypeError as e:
assert str(e) == f"encode() argument 'encoding' must be str, not {expected_type}", (
f'encode({bad!r}) wrong type: {e}'
)
try:
'hello'.encode('utf-8', bad)
assert False, f'encode(errors={bad!r}) should error'
except TypeError as e:
assert str(e) == f"encode() argument 'errors' must be str, not {expected_type}", (
f'encode(errors={bad!r}) wrong type: {e}'
)
assert 'hello'.isidentifier() == True
assert '_hello'.isidentifier() == True
assert '__init__'.isidentifier() == True
assert 'hello123'.isidentifier() == True
assert ''.isidentifier() == False
assert '123hello'.isidentifier() == False
assert 'hello world'.isidentifier() == False
assert 'hello-world'.isidentifier() == False
assert 'class'.isidentifier() == True assert 'café'.isidentifier() == True
assert 'á'.isidentifier() == True assert 'Ω'.isidentifier() == True
assert '3'.isidentifier() == False
assert 'Hello World'.istitle() == True
assert 'Hello'.istitle() == True
assert 'HELLO'.istitle() == False
assert 'hello'.istitle() == False
assert ''.istitle() == False
assert 'Hello world'.istitle() == False
assert '123'.istitle() == False
assert 'Hello 123 World'.istitle() == True
assert "They'Re".istitle() == True
assert '٠١٢٣٤٥٦٧٨٩'.isdecimal() == True
assert '0123456789'.isdecimal() == True
assert '०१२३४५६७८९'.isdecimal() == True
assert '²'.isdecimal() == False
assert '½'.isdecimal() == False
assert '²³'.isdigit() == True
assert '₀₁₂₃₄₅₆₇₈₉'.isdigit() == True
assert '0123456789'.isdigit() == True
assert '٠١٢٣٤٥٦٧٨٩'.isdigit() == True
assert '½'.isdigit() == False
assert '½'.isnumeric() == True
assert '²'.isnumeric() == True
assert '٠١٢٣٤٥٦٧٨٩'.isnumeric() == True
assert '0123456789'.isnumeric() == True
assert '\thello'.expandtabs() == ' hello'
assert ''.expandtabs() == ''
assert 'no tabs here'.expandtabs() == 'no tabs here'
assert '\thello'.expandtabs(4) == ' hello'
assert '\thello'.expandtabs(8) == ' hello'
assert '\thello'.expandtabs(1) == ' hello'
assert 'a\tb'.expandtabs() == 'a b'
assert 'ab\tcd'.expandtabs() == 'ab cd'
assert 'abcdefg\th'.expandtabs() == 'abcdefg h'
assert 'abcdefgh\ti'.expandtabs() == 'abcdefgh i'
assert 'a\tb\tc'.expandtabs(4) == 'a b c'
assert '\thello'.expandtabs(0) == 'hello'
assert 'a\tb\tc'.expandtabs(0) == 'abc'
assert '\thello'.expandtabs(-1) == 'hello'
assert 'a\tb\nc\td'.expandtabs(4) == 'a b\nc d'
assert '\t\n\t'.expandtabs(4) == ' \n '
assert 'a\tb\rc\td'.expandtabs(4) == 'a b\rc d'
assert '\thello'.expandtabs(tabsize=4) == ' hello'
try:
'hello'.expandtabs(wrong=4)
assert False, 'expandtabs wrong kwarg should raise'
except TypeError as e:
assert str(e) == "expandtabs() got an unexpected keyword argument 'wrong'", f'wrong: {e}'
try:
'hello'.expandtabs(4, tabsize=8)
assert False, 'expandtabs pos + kwarg should raise'
except TypeError as e:
assert str(e) == 'expandtabs() takes at most 1 argument (2 given)', f'dup: {e}'
try:
'hello'.expandtabs(4, 5)
assert False, 'expandtabs too many args should raise'
except TypeError as e:
assert str(e) == 'expandtabs() takes at most 1 argument (2 given)', f'toomany: {e}'
try:
'hello'.splitlines(wrong=True)
assert False, 'splitlines wrong kwarg should raise'
except TypeError as e:
assert str(e) == "splitlines() got an unexpected keyword argument 'wrong'", f'wrong: {e}'
try:
'hello'.splitlines(True, keepends=False)
assert False, 'splitlines pos + kwarg should raise'
except TypeError as e:
assert str(e) == 'splitlines() takes at most 1 argument (2 given)', f'dup: {e}'