Size of String in Memory - Python Last Updated : 17 Jan, 2025 Comments Improve Suggest changes Like Article Like Report There can be situations in which we require to get the size that the string takes in bytes using Python which is usually useful in case one is working with files. Let's discuss certain ways in which this can be performed.Using sys.getsizeof()This task can also be performed by one of the system calls, offered by Python as in sys function library, the getsizeof function can get us the size in bytes of desired string. Python import sys s = "geekforgeeks" # getting the size of the string in bytes res = sys.getsizeof(s) print(str(res)) Output61 Note : Here 61 bytes result comes from the string "geekforgeeks", which takes 12 bytes for its content, and around 49-50 bytes for Python's internal overhead. The total memory size is 61 bytes.Using encode() The simplest and initial method that comes to mind is to convert the string into a byte format and then extract its size. Python s= "geekforgeeks" # getting the size of the string in bytes res = len(s.encode('utf-8')) print(str(res)) Output12 Using memoryview()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. Python s= "geekforgeeks" # getting size of string in bytes res = memoryview(s.encode('utf-8')).nbytes print(str(res)) Output12 Comment More infoAdvertise with us Next Article Size of String in Memory - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Find Length of String in Python In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. Pythona = "geeks" print(len(a)) Output5 Using for loop and 'in' operatorA string can be iterated over, directly in a for loop. 2 min read String Interning in Python String interning is a memory optimization technique used in Python to enhance the efficiency of string handling. In Python, strings are immutable, meaning their values cannot be changed after creation. String interning, or interning strings, involves reusing existing string objects rather than creat 2 min read Find the size of a list - Python In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using get 2 min read Python Memory Consumption: Strings vs Lists Programming memory use is an important consideration, particularly when working with big datasets or resource-intensive programs. Writing effective Python code requires knowing how various data structures affect memory utilization. This article examines how lists and strings use memory differently i 5 min read Find the size of a Set in Python A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Pythonâs set class represents the mathematical notion of a set. The size of a set means the amount of memory (in bytes) occupied by a set object. In this article, we will learn various ways to get th 2 min read Python - Avoid Spaces in string length When working with strings in Python, we may sometimes need to calculate the length of a string excluding spaces. The presence of spaces can skew the length when we're only interested in the number of non-space characters. Let's explore different methods to find the length of a string while ignoring 3 min read Python - Longest Substring Length of K Given a String and a character K, find longest substring length of K. Input : test_str = 'abcaaaacbbaa', K = b Output : 2 Explanation : b occurs twice, 2 > 1. Input : test_str = 'abcaacccbbaa', K = c Output : 3 Explanation : Maximum times c occurs is 3. Method #1: Using loop This is brute way to 7 min read Find the Size of a Tuple in Python There are several ways to find the "size" of a tuple, depending on whether we are interested in the number of elements or the memory size it occupies. For Example: if we have a tuple like tup = (10, 20, 30, 40, 50), calling len(tup) will return 5, since there are five elements in the tuple.Using len 3 min read Iterate over words of a String in Python In this article, weâll explore different ways to iterate over the words in a string using Python.Let's start with an example to iterate over words in a Python string:Pythons = "Learning Python is fun" for word in s.split(): print(word)OutputLearning Python is fun Explanation: Split the string into w 2 min read How to copy a string in Python Creating a copy of a string is useful when we need a duplicate of a string to work with while keeping the original string intact, strings in Python are immutable which means they can't be altered after creation, so creating a copy sometimes becomes a necessity for specific use cases.Using SlicingSli 2 min read Like