Compare commits
2
Commits
716e254905
...
74545a6ad1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74545a6ad1 | ||
|
|
b6be9c9488 |
@@ -0,0 +1,20 @@
|
|||||||
|
# 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)
|
||||||
Reference in New Issue
Block a user