Files
30-seconds-of-code/snippets/byte_size.md
2019-10-06 19:48:51 +00:00

19 lines
322 B
Markdown

---
title: byte_size
tags: string, beginner
---
Returns the length of a string in bytes.
Use `input_string.encode('utf-8')` to encode the given string and return its length.
```py
def byte_size(input_string):
return len(input_string.encode('utf-8'))
```
```py
byte_size('😀') # 4
byte_size('Hello World') # 11
```