Skip to content

String Manipulation

In Python, a String (str) is a sequence of Unicode characters. Whether you are building a web server, analyzing data, or writing a simple script, text processing will be your most frequent task.

In Python, strings are not just “arrays of characters”—they are sophisticated objects with a rich set of behaviors.


Python offers multiple ways to define strings, each serving a different purpose.

  • Single/Double Quotes: 'Hi' or "Hi". Use double quotes if your text contains a single quote: "It's Python".
  • Triple Quotes: """...""". Used for multiline strings. These preserve all whitespace and newlines exactly as typed.

Strings in Python are Immutable. This means that once a string object is created in memory, its contents can never be changed.

immutability_fail.py
name = "Python"
# name[0] = "C" # Raises TypeError: 'str' object does not support item assignment

Because strings are immutable, every time you do s1 + s2, Python must:

  1. Allocate a brand new block of memory large enough for both.
  2. Copy the characters from s1.
  3. Copy the characters from s2.

Best Practice: If you are joining many strings (e.g., inside a loop), do not use +. Instead, collect them in a list and use "".join(list_of_strings). This is significantly faster ($O(n)$ vs $O(n^2)$).


Strings are sequences, meaning every character has a position (index).

Python
012345
-6-5-4-3-2-1
  • start: Inclusive.
  • stop: Exclusive (up to but not including).
  • step: The increment (negative reverses the string).
slicing_pro.py
msg = "Technogic"
print(msg[0:4]) # "Tech"
print(msg[6:]) # "gic"
print(msg[::-1]) # "cigonhceT" (Reverse)

Introduced in Python 3.6, f-strings (formatted string literals) are the fastest and most readable way to interpolate variables.

fstrings.py
user = "Alice"
score = 98.5
print(f"User {user.upper()} scored {score:.0f}%")
# Output: User ALICE scored 99%

Under the Hood: f-strings are evaluated at runtime. This means you can put any valid Python expression inside the curly braces {}.


To save memory, Python performs String Interning. If you create two identical short strings that look like identifiers (no spaces/symbols), Python may point both variables to the same memory address.

interning.py
a = "hello"
b = "hello"
print(a is b) # True (Shared memory!)
c = "hello world!"
d = "hello world!"
print(c is d) # False (Too complex to intern automatically)

MethodUse Case
.strip()Removes whitespace from both ends.
.split()Breaks a string into a list based on a delimiter.
.replace()Swaps all occurrences of one substring for another.
.find()Returns the index of a substring (or -1 if not found).
.encode()Converts the string to bytes (usually UTF-8).
data_cleaning.py
line = " ID, NAME, ROLE "
# Chaining methods
parts = line.strip().lower().split(", ")
print(parts) # ['id', 'name', 'role']