20 lines
854 B
Python
20 lines
854 B
Python
# Question7:
|
|
# Difference:
|
|
# 1. Byte objects are sequence of Bytes, whereas Strings are sequence of characters.
|
|
# 2. Byte objects are in machine readable form internally; Strings are only in human readable form.
|
|
# 3. strings (str) support various character encodings, such as UTF-8, ASCII, and more. Bytes (bytes), on the other hand, represent raw binary data and can hold any byte values.
|
|
## Emojis can affect the size difference.
|
|
import sys
|
|
string_size = sys.getsizeof("Hello")
|
|
bytes_size = sys.getsizeof(b"Hello")
|
|
emoji_size = sys.getsizeof("😀")
|
|
print(f"String size: {string_size} bytes")
|
|
print(f"Bytes size: {bytes_size} bytes")
|
|
print(f"emoji size: {emoji_size} bytes")
|
|
|
|
# python routine:
|
|
def print_string_as_numbers(string):
|
|
for char in string:
|
|
print(ord(char))
|
|
input_string = "Hello, world!"
|
|
print_string_as_numbers(input_string) |