Skip to main content

Strings (str)

We mentioned strings in the Variables and Data Types section, but now we'll explore them in more depth. Strings are sequences of characters used to represent text. They are immutable, meaning once a string is created, its contents cannot be changed.

What are Strings?

Strings in Python are sequences of characters used to represent text. They are immutable, meaning once a string is created, its contents cannot be changed. Strings were introduced briefly in the Variables and Data Types section, but now we'll explore them in more depth.

tip

If you wish to learn more about immutability, read "mutability" in the computer science section .

Creating Strings

There are several ways to create strings in Python:

  1. Using quotes:

    single_quoted = 'Hello'
    double_quoted = "World"
    triple_quoted = '''This is a
    multi-line string.'''

    Note: Single, double, and triple quotes are interchangeable, but triple quotes are often used for multi-line strings or docstrings.

  2. Using the str() function:

    number = 42
    number_string = str(number) # Converts integer 42 to string "42"
  3. String concatenation:

    first = "Hello"
    second = "World"
    combined = first + " " + second # Results in "Hello World"

String Properties

  1. Immutability: Strings are immutable. This means you can't change a string once it's created.

    text = "Python"
    # This will raise an error:
    # text[0] = "J"
  2. Indexing: Access individual characters using square brackets:

    text = "Python"
    first_char = text[0] # 'P'
    last_char = text[-1] # 'n'
  3. Slicing: Extract a portion of a string:

    text = "Python"
    slice = text[1:4] # "yth"
  4. Length: Use the len() function to get the number of characters:

    text = "Python"
    length = len(text) # 6

Escape Characters

Escape characters allow you to include special characters in strings:

with_quote = "He said, \"Hello!\""
with_newline = "First line\nSecond line"

Common escape characters:

  • \n: Newline
  • \t: Tab
  • \\: Backslash
  • \": Double quote
  • \': Single quote

Raw Strings

Raw strings treat backslashes as literal characters:

raw_string = r"C:\Users\Username"

This is useful for file paths or regular expressions.

String Methods

Python provides many built-in methods for string manipulation:

text = "  Python Programming  "
lower_case = text.lower()
upper_case = text.upper()
stripped = text.strip()
replaced = text.replace("Python", "Java")

These methods create new strings rather than modifying the original, due to string immutability.

String Formatting

There are several ways to format strings:

  1. f-strings (Python 3.6+):

    name = "Alice"
    age = 30
    print(f"{name} is {age} years old.")
  2. format() method:

    print("{} is {} years old.".format(name, age))
  3. %-formatting (older style):

    print("%s is %d years old." % (name, age))

String Immutability and Operations

Immutability Explained

Strings in Python are immutable, which means that once a string object is created, its content cannot be changed. However, it's important to understand how this works in practice, especially when it seems like we're modifying strings.

  1. String Assignment: When you assign a new value to a string variable, you're not changing the original string object. Instead, you're creating a new string object and making the variable reference this new object.

    text = "Hello"
    print(id(text)) # Let's say this prints 140230416613680

    text = "World"
    print(id(text)) # This will print a different number, e.g., 140230416613712

    In this example, text first refers to the string object "Hello". When we assign "World" to text, a new string object is created, and text now refers to this new object. The original "Hello" object remains unchanged (and will be garbage collected if no other references to it exist).

  2. String Concatenation: When you concatenate strings or use methods that seem to modify a string, you're actually creating a new string object.

    greeting = "Hello"
    name = "Alice"
    full_greeting = greeting + " " + name

    Here, full_greeting is a new string object containing "Hello Alice". The original greeting and name strings are unchanged.

  3. String Methods: Methods that appear to modify strings actually return new string objects.

    text = "python"
    upper_text = text.upper()

    print(text) # Still prints "python"
    print(upper_text) # Prints "PYTHON"

    The upper() method doesn't change text; it creates and returns a new string object.

Memory Efficiency

Python optimizes memory usage for strings in several ways:

  1. String Interning: For small strings, Python may use string interning, where it reuses string objects instead of creating new ones.

    a = "hello"
    b = "hello"
    print(a is b) # Often True, but not guaranteed for all strings
  2. String Concatenation Optimization: For operations like building a string in a loop, it's more efficient to use methods like str.join() or list comprehensions instead of repeated concatenation.

    # Less efficient
    result = ""
    for i in range(1000):
    result += str(i)

    # More efficient
    result = ''.join(str(i) for i in range(1000))

Practical Implications

Understanding string immutability is crucial for writing efficient Python code:

  1. Be cautious when performing many string operations in a loop, as each operation creates a new string object.
  2. Use appropriate methods for string manipulation tasks (e.g., str.join() for concatenating many strings).
  3. When you need to make many changes to a string, consider using a mutable sequence type like a list of characters, then join them back into a string when finished.

By understanding these concepts, you can write more efficient and effective code when working with strings in Python.