The original description was slightly misleading by saying that utf-8 will encode and find the length, whereas in reality the length was produced by len.
16 lines
285 B
Markdown
16 lines
285 B
Markdown
### byte_size
|
|
|
|
Returns the length of a string in bytes.
|
|
|
|
`utf-8` encodes a given string, then `len` finds the length of the encoded string.
|
|
|
|
```python
|
|
def byte_size(string):
|
|
return(len(string.encode('utf-8')))
|
|
```
|
|
|
|
```python
|
|
byte_size('😀') # 4
|
|
byte_size('Hello World') # 11
|
|
```
|