String Manipulation

String methods, formatting, regex basics

6 free lessons available

Start here

String Creation and Immutability

You're cleaning a dataset, standardizing product names. You write a script to convert 'Apple iPhone' to 'iPhone' and run it. Later, you find some of the original 'Apple iPhone' entries still exist, seemingly untouched, while others are correctly updated. Why did your script only partially work, leaving behind the old values when you thought you were modifying them?

This perplexing behavior points to a core characteristic of strings in Python: their immutability. This lesson will explain how strings are created and why their unchangeable nature is key to understanding such data manipulation quirks.

Crafting Strings: Literals and Constructors

The most common and straightforward way to create a string in Python is by using a string literal. This involves enclosing a sequence of characters within single quotes ('...') or double quotes ("..."). Python treats any text enclosed this way as a string object, ready for use.

pythonCreating Strings with Literals

While literals are ideal for static text, the str() constructor provides a more explicit way to create strings, especially when converting other data types. Calling str() without arguments creates an empty string. When an argument is provided, str() attempts to convert that object into its string representation.

pythonUsing the str() Constructor
Comparing String Literals and the str() Constructor
FeatureString Literalstr() Constructor
SyntaxEnclosed in quotes ('...' or "...")Function call: str(object) or str()
FlexibilityStatic, fixed textDynamic conversion of other types; creates empty strings
Typical UseDefining constant strings, messages, namesType conversion, explicit string creation
ReadabilityOften more concise for simple stringsExplicit, clear for conversions
EfficiencyGenerally more efficient for static stringsInvolves a function call, slight overhead for conversions
Choose literals for fixed text and the str() constructor for dynamic type conversions.
Check Your Understanding
When creating a simple, fixed message like 'Access Denied', which method is generally preferred for clarity and conciseness?

Strings: Unchangeable Once Born

A core property of strings in Python is their immutability. This means that once a string object has been created in memory, its sequence of characters cannot be changed. Any operation that appears to modify a string, such as concatenation or replacing characters, does not alter the original string object. Instead, these operations always produce a new string object with the desired content, leaving the original string untouched in its memory location.

pythonTracking String Identity with id()
⚠️ Reassignment vs. Modification

It's easy to confuse variable reassignment with in-place object modification. When you write my_string = my_string + 'suffix', you are not changing the string object that my_string used to point to. Instead, you are creating a new string object and then making my_string point to this new object. The original string object remains unchanged in memory until Python's garbage collector removes it.

Why Immutability Matters in Your Code

String immutability has several significant practical consequences for Python developers. For memory management, Python can perform string interning, where identical string literals are often stored only once, saving memory. This also makes strings safe to use as dictionary keys or set members, as their hash value (derived from their content) remains constant throughout their lifetime. However, repeated string concatenation using + can be inefficient, as each operation creates a new string object, potentially leading to many temporary objects and increased memory churn.

pythonEfficient String Building with str.join()
Try It Yourself
The previous example showed how str.join() is more efficient for building long strings. Modify the str.join() example to construct a comma-separated string of product IDs, like 'PROD-001,PROD-002,...,PROD-100'. Use a list comprehension to generate the product IDs before joining.
python
Check Your Understanding
If you have a string s = 'hello' and call s.upper(), what happens to the original s object?
Key Takeaways
  • Strings are created using literals (e.g., 'text') for static content or the str() constructor for dynamic type conversion.

  • Python strings are immutable: once created, their content cannot be changed.

  • Operations that appear to modify a string (like concatenation or replace()) actually create a new string object in memory.

  • Variables are then reassigned to point to these new string objects, leaving the original object untouched.

  • The id() function can verify immutability by showing different memory addresses for 'modified' strings.

  • Immutability enables optimizations like string interning but makes repeated concatenation (using +) inefficient due to many temporary objects.

  • The 'stubborn string' problem from the opening scenario occurred because your script's 'modifications' created new strings, but the original references in the dataset were not always updated to point to these new strings, leaving old values behind.

Open this lesson on its own page →

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free