String Interning in Python Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 creating new ones with the same value. This process can lead to memory savings and improved performance in certain scenarios. Ways For String Interning in PythonBelow, are the example of String Interning in Python. Identical String ValuesString ConcatenationString SlicingExplicit InterningDictionary KeysString Interning Using Identical String ValuesIn this example, both str1 and str2 have the same value, "hello". String interning ensures that identical string literals refer to the same memory location, making the is operator return True. Python3 str1 = "hello" str2 = "hello" print(str1 is str2) OutputTrueString Interning by String ConcatenationWhen concatenating strings using the + operator, Python automatically interns the resulting string if the concatenated values are string literals. This is a subtle optimization that enhances performance. Python3 str1 = "hello" str2 = "world" concatenated_str = str1 + str2 print(concatenated_str is "helloworld") OutputFalseString Interning by String SlicingString slices also benefit from interning. The resulting substring shares memory with the original string, reducing the overall memory footprint. Python3 str1 = "python" substring = str1[1:4] print(substring is "yth") OutputFalseString Interning Using Explicit InterningPython provides the intern() function to explicitly intern strings. In this example, both str1 and str2 reference the same interned string, ensuring that their memory locations are identical. Python3 import sys str1 = "apple" str2 = sys.intern("apple") print(str1 is str2) OutputTrueString Interning by Dictionary KeysWhen using string literals as dictionary keys, Python automatically interns the keys. This behavior ensures efficient dictionary lookups and conserves memory by reusing string objects. Python3 dictionary = {"key1": "value1", "key2": "value2"} key_literal = "key1" print(key_literal is "key1") print(dictionary[key_literal]) OutputTrue value1ConclusionUnderstanding string interning in Python is crucial for optimizing memory usage and improving the performance of string operations. By recognizing scenarios where interning occurs automatically and leveraging explicit interning when necessary, developers can write more memory-efficient code. Comment More infoAdvertise with us Next Article String Interning in Python harshitmongre Follow Improve Article Tags : Python Python 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 Insert a number in string - Python We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num 2 min read List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona = 2 min read Python - Words Lengths in String We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6].Using List 2 min read Word location in String - Python Word location in String problem in Python involves finding the position of a specific word or substring within a given string. This problem can be approached using various methods in Python, such as using the find(), index() methods or by regular expressions with the re module.Using str.find()str.fi 4 min read Python | Add one string to another The concatenation of two strings has been discussed multiple times in various languages. But the task is how to add to a string in Python or append one string to another in Python. Example Input: 'GFG' + 'is best' Output: 'GFG is best' Explanation: Here we can add two string using "+" operator in Py 5 min read How to Initialize a String in Python In Python, initializing a string variable is straightforward and can be done in several ways. Strings in Python are immutable sequences of characters enclosed in either single quotes, double quotes or triple quotes. Letâs explore how to efficiently initialize string variables.Using Single or Double 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 In-Place String Modifications in Python Strings are immutable in Python, meaning their values cannot be modified directly after creation. However, we can simulate in-place string modifications using techniques such as string slicing and conversion to mutable data types like lists.Using String SlicingString slicing is one of the most effic 2 min read Python program to find String in a List Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e 3 min read Like