Introduction
In Python, a string is a sequence of characters. Each character in a string takes up a certain amount of memory. The amount of memory taken up by a string is known as its byte size. In this challenge, you will write a function that takes a string as input and returns its byte size.
Byte Size of String
Write a function byte_size(s) that takes a string s as input and returns its byte size. The byte size of a string is the number of bytes required to store the string in memory. To calculate the byte size of a string, you need to encode the string using a specific encoding scheme. In this lab, you will use the UTF-8 encoding scheme.
To calculate the byte size of a string, you can follow these steps:
- Encode the string using the UTF-8 encoding scheme.
- Get the length of the encoded string.
Your function should return the length of the encoded string.
def byte_size(s):
return len(s.encode('utf-8'))
byte_size('😀') ## 4
byte_size('Hello World') ## 11
Summary
In this challenge, you learned how to calculate the byte size of a string in Python. You wrote a function that takes a string as input and returns its byte size by encoding the string using the UTF-8 encoding scheme and getting the length of the encoded string.