🌐
Artem Golubin
rushter.com › blog › python-strings-and-memory
How Python saves memory when storing strings | Artem Golubin
August 10, 2018 - You said that inserting a single emoji into a text of size 10GB of ASCII will increase the size by a factor of 4. But in Python each character is encoded using 1-4 bytes depending on the character it is representing.
Discussions

What does it mean for the size of a char variable to be 1 byte?
Depending on the language, a char is only one character. Characters can be encoded in various encodings, the smallest being ASCII which fits exactly in a single byte. Hence, a char encoded in ASCII is a single byte. Yes, if you use a variable of type char and a variable of type double it will take up 9 bytes of memory (provided that a double is 8 bytes). More on reddit.com
🌐 r/learnprogramming
7
2
May 14, 2021
How to get the size (length) of a string in Python - Stack Overflow
A. The len() function in Python 2 returns count of bytes allocated to store encoded characters in a str object. More on stackoverflow.com
🌐 stackoverflow.com
Python: Get size of string in bytes - Stack Overflow
and even ASCII exclamation point ... max char width == 2. The same fixed increase by 4 bytes happens for string with more complicated symbols. Current adaptive representation had introduced in Python 3.3, and in general it is still not changed (3.14 is coming this year, but no much changes in str) except some optimizations for initial C structure size and having ... More on stackoverflow.com
🌐 stackoverflow.com
Do there exist unicoded strings where len()/python and len()/rust are different?
I understand that char != byte, that a char can be 1-4 bytes depending on what it is trying to encode. I am a bit confused on what counts as a char. Sometimes, what "visually looks like one char" ends up being two chars (base + accent). I am in a sitaution where I am dealing with unicode strings ... More on users.rust-lang.org
🌐 users.rust-lang.org
7
0
January 23, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-size-of-string-in-memory
Size of String in Memory - Python - GeeksforGeeks
July 11, 2025 - To get the number of bytes in a string using a memoryview, we can use the nbytes attribute of the memoryview object. This attribute returns the total size of the memoryview's data in bytes.
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string. Get the character at position 1 (remember that the first character has ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get the Length of a String (number of Characters) in Python | note.nkmk.me
April 30, 2025 - In Python, you can get the length of a string (str), which is the number of characters, using the built-in len() function. Built-in Functions - len() — Python 3.13.3 documentation Get the length of a ...
🌐
30 Seconds of Code
30secondsofcode.org › home › python › byte size
Byte size of a Python string - 30 seconds of code
August 16, 2024 - While the len() function returns the number of characters in a string, it doesn't account for the number of bytes required to encode it. To get the byte size of a string, you can use the str.encode() method to encode the string and then return ...
🌐
TutorialsPoint
tutorialspoint.com › How-to-get-the-size-of-a-string-in-Python
How to get the size of a string in Python
December 6, 2020 - To get the length of a string, just pass the string to the len() call. exampleprint(len('Hello World!'))Output12If you want the size of the string in bytes, you can use the getsizeof() method from the sys module.
🌐
AskPython
askpython.com › python › built-in-methods › variables-memory-size-in-python
Variable's memory size in Python - AskPython
February 8, 2026 - So the size of a single character in Python is 50 bytes. A single character in C consumes a memory space of 1 byte. So storing a single character in Python takes 50 times more space than storing a single character in C.
Find elsewhere
🌐
CompleteEra
completeera.com › char-is-how-many-bytes-understanding-the-size
Char is How Many Bytes: Understanding the Size - CompleteEra
April 26, 2026 - A char is **1 byte in ASCII**, but **variable-width in UTF-8** (1–4 bytes). UTF-8 is the **best choice for global text** (supports emojis, non-Latin scripts). UTF-16 is **fixed-width (2 bytes)** but wastes space for ASCII-only text. Always **specify encoding** in code and databases to avoid ...
🌐
Finxter
blog.finxter.com › how-to-get-the-size-of-a-string-in-python
How to Get the Size of a String in Python? – Be on the Right Side of Change
May 8, 2021 - Here are the three examples: my_string = "what is my length?" print(len(my_string)) # 18 my_string = "hello" print(len(my_string)) # 5 my_string = "" print(len(my_string)) # 0 · Here’s an introduction into Python’s built-in len() function: Just for comprehensibility, I want to address ...
Top answer
1 of 3
322

If you are talking about the length of the string, you can use len():

>>> s = 'please answer my question'
>>> len(s)  # number of characters in s
25

If you need the size of the string in bytes, you need sys.getsizeof():

>>> import sys
>>> sys.getsizeof(s)
58

Also, don't call your string variable str. It shadows the built-in str() function.

2 of 3
49

Python 3:

user225312's answer is correct:

A. To count number of characters in str object, you can use len() function:

>>> print(len('please anwser my question'))
25

B. To get memory size in bytes allocated to store str object, you can use sys.getsizeof() function

>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50

Python 2:

It gets complicated for Python 2.

A. The len() function in Python 2 returns count of bytes allocated to store encoded characters in a str object.

Sometimes it will be equal to character count:

>>> print(len('abc'))
3

But sometimes, it won't:

>>> print(len('йцы'))  # String contains Cyrillic symbols
6

That's because str can use variable-length encoding internally. So, to count characters in str you should know which encoding your str object is using. Then you can convert it to unicode object and get character count:

>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 
3

B. The sys.getsizeof() function does the same thing as in Python 3 - it returns count of bytes allocated to store the whole string object

>>> print(getsizeof('йцы'))
27
>>> print(getsizeof('йцы'.decode('utf8')))
32
🌐
W3Schools
w3schools.com › python › gloss_python_string_length.asp
Python String Length
The len() function returns the length of a string: a = "Hello, World!" print(len(a)) Try it Yourself » · Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative ...
🌐
LinkedIn
linkedin.com › all › engineering › programming
How do you determine the length of a string in Python?
January 16, 2024 - Picture len() as a ruler measuring your string. Simply pass your string to len(), like len("Hello World"), and it returns the number of characters. It's the go-to, straightforward way to measure string length in Python, quick and efficient for ...
🌐
Rust Programming Language
users.rust-lang.org › t › do-there-exist-unicoded-strings-where-len-python-and-len-rust-are-different › 24537
Do there exist unicoded strings where len()/python and len()/rust are different? - The Rust Programming Language Forum
January 23, 2019 - I understand that char != byte, that a char can be 1-4 bytes depending on what it is trying to encode. I am a bit confused on what counts as a char. Sometimes, what "visually looks like one char" ends up being two chars (base + accent).
🌐
Reddit
reddit.com › r/learnpython › character length to byte length or vice versa
r/learnpython on Reddit: character length to Byte length or vice versa
January 13, 2023 -

[SOLVED]

Hey, I am trying to create a password generator where I allow the user to enter the length of their password and I return to them a password of their desired length.

I am creating the password using the token_urlsafe() function from py's secrets module. But the function takes in byte length and not character length, so when I give it a length of 16, it gives me a password with 22 characters, this is obviously not user friendly so I want to find a way to convert the user's desired character length to byte length before sending it to token_urlsafe() so it creates a string with character length equal to what the user desires

🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-string.html
Python Strings
The formal name of the string type in Python is "str". The str() function serves to convert many values to a string form. This code computes the str form of the number 123: ... Looking carefully at the values, 123 is a number, while '123' is a string length-3, made of the three chars '1' '2' and '3'.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.char.chararray.size.html
numpy.char.chararray.size — NumPy v2.4 Manual
char.chararray.size# Number of elements in the array. Equal to np.prod(a.shape), i.e., the product of the array’s dimensions. Notes · a.size returns a standard arbitrary precision Python integer.
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1202 › handouts › py-string.html
Py Strings
... Each character in a string is drawn from the unicode character set, which includes the "characters" or pretty much every language on earth, plus many emojis. See the unicode section below for more information. The len() function returns the length of a string, the number of chars in it.