assert 'hello' + ' ' + 'world' == 'hello world'
assert '' + 'test' == 'test'
assert 'test' + '' == 'test'
assert '' + '' == ''
assert 'a' + 'b' + 'c' + 'd' == 'abcd'
s = 'hello'
s += ' world'
assert s == 'hello world'
s = 'test'
s += ''
assert s == 'test'
s = 'a'
s += 'b'
s += 'c'
assert s == 'abc'
s = 'ab'
s += s
assert s == 'abab'
assert len('') == 0
assert len('a') == 1
assert len('hello') == 5
assert len('hello world') == 11
assert len('caf\xe9') == 4
assert repr('') == "''"
assert str('') == ''
assert repr('hello') == "'hello'"
assert str('hello') == 'hello'
assert repr('hello "world"') == '\'hello "world"\''
assert str('hello "world"') == 'hello "world"'
assert 'ab' * 3 == 'ababab'
assert 3 * 'ab' == 'ababab'
assert 'x' * 0 == ''
assert 'x' * -1 == ''
assert '' * 5 == ''
assert 'a' * 1 == 'a'
s = 'ab'
s *= 3
assert s == 'ababab'
s = 'x'
s *= 0
assert s == ''
assert ','.join(['a', 'b', 'c']) == 'a,b,c'
assert ''.join(['a', 'b', 'c']) == 'abc'
assert '-'.join([]) == ''
assert ','.join(['only']) == 'only'
assert ' '.join(('hello', 'world')) == 'hello world'
assert ','.join('abc') == 'a,b,c'
sep = '-'
assert sep.join(['a', 'b']) == 'a-b'
s = str('.')
assert s.join(['a', 'b']) == 'a.b'
mixed = ['hello', str('world')]
assert ' '.join(mixed) == 'hello world'
assert 'hello'[0] == 'h'
assert 'hello'[1] == 'e'
assert 'hello'[4] == 'o'
assert 'hello'[-1] == 'o'
assert 'hello'[-2] == 'l'
assert 'hello'[-5] == 'h'
assert 'a'[0] == 'a'
assert 'a'[-1] == 'a'
s = 'café'
assert s[0] == 'c'
assert s[1] == 'a'
assert s[2] == 'f'
assert s[3] == 'é'
assert s[-1] == 'é'
s = '日本語'
assert s[0] == '日'
assert s[1] == '本'
assert s[2] == '語'
assert s[-1] == '語'
s = 'a🎉b'
assert s[0] == 'a'
assert s[1] == '🎉'
assert s[2] == 'b'
s = str('hello')
assert s[0] == 'h'
assert s[-1] == 'o'
s = 'abc'
i = 1
assert s[i] == 'b'
s = 'abc'
assert s[False] == 'a'
assert s[True] == 'b'
assert 'a' < 'b'
assert 'b' > 'a'
assert 'a' <= 'a'
assert 'a' <= 'b'
assert 'b' >= 'b'
assert 'b' >= 'a'
assert not ('b' < 'a'), 'str not < str'
assert not ('a' > 'b'), 'str not > str'
assert 'a' < 'aa'
assert 'ab' < 'b'
assert '' < 'a'
assert 'abc' > 'ab'
assert 'café' < 'cafë'
assert 'z' < 'é'
assert '日' < '本'
assert '😀' < '😁'
assert sorted('cba') == ['a', 'b', 'c']
assert sorted(['b', 'c', 'a']) == ['a', 'b', 'c']
assert sorted(['café', 'cafë', 'cafa']) == ['cafa', 'café', 'cafë']
assert sorted(['bb', 'a', 'ba']) == ['a', 'ba', 'bb']
assert str(object=42) == '42'
assert str(object='hello') == 'hello'
assert str(object=True) == 'True'
assert str(object=[1, 2]) == '[1, 2]'
assert str(object=None) == 'None'
try:
str(wrong=42)
assert False, 'str wrong kwarg should raise'
except TypeError as e:
assert str(e) == "str() got an unexpected keyword argument 'wrong'", f'wrong: {e}'
try:
str(42, object=42)
assert False, 'str pos + kwarg should raise'
except TypeError as e:
assert str(e) == "argument for str() given by name ('object') and position (1)", f'dup: {e}'
assert str(b'x', 'utf-8') == 'x'
assert str(b'x', encoding='utf-8') == 'x'
assert str(object=b'x', encoding='utf-8') == 'x'
assert str(b'\xe2\x82\xac', 'utf-8') == '€'
assert str(b'\xff', 'utf-8', 'replace') == '�'
assert str(b'\xff', errors='replace') == '�'
assert str(b'x', errors='strict') == 'x'
assert str(encoding='utf-8') == ''
assert str(errors='strict') == ''
assert str(b'x') == "b'x'"
try:
str(b'\xff', 'utf-8')
assert False, 'expected UnicodeDecodeError'
except UnicodeDecodeError as e:
assert str(e) == "'utf-8' codec can't decode byte 0xff in position 0: invalid start byte"
try:
str('x', 'utf-8')
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == 'decoding str is not supported'
try:
str('x', errors='strict')
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == 'decoding str is not supported'
try:
str(1, 'utf-8')
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == 'decoding to str: need a bytes-like object, int found'
try:
str(None, 'utf-8')
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == 'decoding to str: need a bytes-like object, NoneType found'
try:
str(b'x', 1)
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == "str() argument 'encoding' must be str, not int"
try:
str(b'x', None)
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == "str() argument 'encoding' must be str, not NoneType"
try:
str(b'x', 'utf-8', 1)
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == "str() argument 'errors' must be str, not int"
try:
str(b'x', 'bogus')
assert False, 'expected LookupError'
except LookupError as e:
assert str(e) == 'unknown encoding: bogus'
try:
str(b'x', 'utf-8', 'strict', 1)
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == 'str expected at most 3 arguments, got 4'
try:
str(b'x', 'utf-8', 'strict', 'x', bogus=1)
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == 'str() takes at most 3 arguments (5 given)'
try:
str(b'x', 'utf-8', encoding='q')
assert False, 'expected TypeError'
except TypeError as e:
assert str(e) == "argument for str() given by name ('encoding') and position (2)"
assert 'bc' in 'abc'
assert 'abc' in 'abc'
assert '' in 'abc'
assert 'x' not in 'abc'
try:
1 in 'abc'
assert False, 'expected TypeError for a non-str probe'
except TypeError as exc:
assert str(exc) == "'in <string>' requires string as left operand, not int"