Seunghyun Yoo

Posts | Development | About

[EN] Negative index in Python

if __name__ == "__main__":
	a = "12345\x14"
	print(type(a[-1]))
	print(type(a[len(a)-1:]))
	assert(a[-1] == a[len(a)-1:])

	b = b"12345\x14"
	print(type(b[-1]))
	print(type(b[len(b)-1:]))
	assert(b[-1] == b[len(b)-1:])  # assertion error!

I thought b[len(b)-1:] was equivalent to b[-1] but it was not true. The type of b[-1] is int whereas the type of b[len(b)-1:] is a byte array. For a string array, there was no difference.