54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
# Name three practical differences between Python strings and bytes.
|
|||
|
|
# Using sys.getsizeof, see how the sizes may be different.
|
||
|
|
# Do emoji change this answer?
|
||
|
|
|
||
|
|
# Encoding
|
||
|
|
# Strings are created using Unicode character points.
|
||
|
|
# While bytes are used to represent actual bytes in memory, which can be encoded using ASCII or UTF-8.
|
||
|
|
|
||
|
|
# Usage
|
||
|
|
# Strings are usually used to represent text.
|
||
|
|
# While bytes are used to represent binary data.
|
||
|
|
|
||
|
|
# Memory Size
|
||
|
|
# Strings take up more memory than bytes, because they are Unicode-based and can contain any character.
|
||
|
|
# While bytes are limited to binary data.
|
||
|
|
|
||
|
|
|
||
|
|
print("---------------------------------------------")
|
||
|
|
print("This is the Memory Size test print out:")
|
||
|
|
|
||
|
|
|
||
|
|
import sys
|
||
|
|
|
||
|
|
my_string = "Hello, world!"
|
||
|
|
my_bytes = b'\x48\x65\x6c\x6c'
|
||
|
|
|
||
|
|
print(sys.getsizeof(my_string)) # Prints 62
|
||
|
|
print(sys.getsizeof(my_bytes)) # Prints 37
|
||
|
|
|
||
|
|
print()
|
||
|
|
print('Test the emoji:')
|
||
|
|
|
||
|
|
x = 'pizza' # Prints 54
|
||
|
|
y = b'pizza' # Prints 38
|
||
|
|
z = '🍕' #Prints 80
|
||
|
|
|
||
|
|
print(sys.getsizeof(x))
|
||
|
|
print(sys.getsizeof(y))
|
||
|
|
print(sys.getsizeof(z))
|
||
|
|
|
||
|
|
|
||
|
|
print("---------------------------------------------")
|
||
|
|
|
||
|
|
print("This is the String to Sequence of Numbers print out:")
|
||
|
|
|
||
|
|
def String_to_SequenceofNumbers():
|
||
|
|
my_string = input("Enter a string: ")
|
||
|
|
num_sequence = [ord(c) for c in my_string]
|
||
|
|
print(num_sequence)
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
String_to_SequenceofNumbers()
|
||
|
|
|
||
|
|
print("---------------------------------------------")
|