ch6s1_StringManipulation

String manipulation is one of the most essential and frequently used skills in Python programming.

Chapter 6: Python Standard Library

Sub-Chapter: String Manipulation — Working with Textual Data

String manipulation is one of the most essential and frequently used skills in Python programming.
From processing user input to formatting logs, cleaning datasets, or generating reports — text operations are everywhere.
Python’s str type and its standard library provide powerful, intuitive, and efficient tools for handling textual data.


🧱 1. String Basics

A string is an immutable sequence of Unicode characters enclosed in either single ' ' or double " " quotes.

text1 = "Hello, World!"
text2 = 'Python is fun!'

Strings are immutable, meaning you cannot modify them directly — any operation creates a new string.

Example:

word = "Python"
word[0] = "J"   # ❌ Error (strings are immutable)
new_word = "J" + word[1:]  # ✅ Creates new string "Jython"

🧩 2. String Concatenation and Repetition

You can combine or repeat strings easily using + and * operators.

name = "Alice"
greeting = "Hello, " + name + "!"
border = "=" * 20
print(greeting)
print(border)

Output:

Hello, Alice!
====================

🎨 3. String Formatting Techniques

Python supports several formatting styles:

name = "Bob"
age = 25
print(f"My name is {name} and I am {age} years old.")

You can include expressions directly:

print(f"Next year I’ll be {age + 1}.")

🧷 b) str.format() Method

template = "My name is {} and I am {} years old."
print(template.format("Alice", 30))

🧷 c) Old-style % Formatting

print("My name is %s and I am %d years old." % ("Charlie", 28))

🧷 d) Alignment and Precision

price = 123.4567
print(f"Price: ${price:10.2f}")  # Width=10, 2 decimal places
print(f"{'Left':<10}|{'Right':>10}")

Output:

Price: $    123.46
Left      |     Right

🧮 4. Escape Sequences and Raw Strings

Python supports escape sequences for special characters:

SequenceMeaning
\nNewline
\tTab
\"Double quote
\'Single quote
\\Backslash

Example:

print("Hello\nWorld\tTabbed")

Raw Strings (r"")

Useful for regex, file paths, or literal backslashes.

path = r"C:\Users\Documents\Python"
print(path)

Output:

C:\Users\Documents\Python

🔠 5. Common String Methods

Python’s string class includes dozens of built-in methods.
Here’s a practical subset:

CategoryMethodsExample
Case Conversionupper(), lower(), title(), capitalize()"hello".upper()"HELLO"
Whitespacestrip(), lstrip(), rstrip()" hi ".strip()"hi"
Searchingfind(), index(), count()"data".find("a")1
Validationisdigit(), isalpha(), isalnum(), isspace()"123".isdigit()True
Modificationreplace(), join(), split()"a,b,c".split(",")['a','b','c']
Start/End Checksstartswith(), endswith()"python".startswith("py")True
Partitioningpartition(), rpartition()"key=value".partition("=")('key', '=', 'value')

Example:

text = "   Python Programming   "
print(text.upper())
print(text.strip())
print(text.replace("Python", "JavaScript"))

Output:

   PYTHON PROGRAMMING   
Python Programming
   JavaScript Programming   

🔍 6. String Slicing and Indexing

Strings are sequences — you can extract substrings using slice notation.

word = "Programming"
print(word[0:6])   # 'Progra'
print(word[-3:])   # 'ing'
print(word[::-1])  # 'gnimmargorP' (reversed)

🔎 7. Searching and Matching Substrings

Basic Methods

sentence = "Python is powerful and popular."
print(sentence.find("powerful"))   # 10
print(sentence.count("p"))         # 3
print(sentence.startswith("Py"))   # True
print(sentence.endswith("."))      # True

Replacing and Splitting

words = sentence.split(" ")
print(words)
joined = "-".join(words)
print(joined)

Output:

['Python', 'is', 'powerful', 'and', 'popular.']
Python-is-powerful-and-popular.

⚙️ 8. Advanced Techniques: Regular Expressions

For more complex text matching, use the re module.

import re

text = "Contact: support@example.com or info@domain.org"
emails = re.findall(r"[\w\.-]+@[\w\.-]+", text)
print(emails)

Output:

['support@example.com', 'info@domain.org']

Regex enables sophisticated searching, validation, and text cleaning — essential for data processing tasks.


🧰 9. Real-World Example — Text Normalization

text = "   Python IS Awesome!!!   "
cleaned = text.strip().lower().replace("!!!", "")
print(cleaned)  # 'python is awesome'

Example: Log Parsing

log = "ERROR 2025-10-26 Connection failed at 10:31PM"
timestamp = log.split()[1]
print(f"Error occurred on {timestamp}")

Output:

Error occurred on 2025-10-26

🧱 10. Working with Unicode and Encoding

Python’s strings are Unicode by default.

word = "café"
encoded = word.encode("utf-8")   # bytes
decoded = encoded.decode("utf-8")
print(encoded, decoded)

Output:

b'caf\xc3\xa9' café

Always specify encoding when reading/writing files to avoid errors across platforms.


🧩 11. Useful Built-in Functions for Strings

FunctionDescriptionExample
len()Length of stringlen("Python") → 6
sorted()Sorts characterssorted("cab") → ['a','b','c']
max(), min()Lexicographic comparisonmax("hello") → 'o'
chr(), ord()Convert between Unicode code and charchr(65) → 'A', ord('A') → 65

🧭 12. Best Practices for String Manipulation

✅ Prefer f-strings for clarity and efficiency.
✅ Avoid manual concatenation in loops (use join()).
✅ Remember: strings are immutable — reuse wisely.
✅ Always sanitize or escape user inputs (especially for file paths or SQL).
✅ For multilingual text, always use Unicode (utf-8 encoding).
✅ Use regex only when simpler string methods are insufficient.


📊 13. UML-Style Overview of String Operations

+---------------------------+
|         str Object        |
+---------------------------+
| + upper()                 |
| + lower()                 |
| + replace(old, new)       |
| + split(delimiter)        |
| + join(iterable)          |
| + find(substring)         |
| + count(substring)        |
| + strip()                 |
| + format(), f""           |
| + encode(), decode()      |
+---------------------------+

All string methods return new string objects, ensuring immutability and thread-safety.


🧾 Summary


By mastering Python’s string manipulation techniques, you’ll gain a solid foundation for handling real-world data — from basic formatting to advanced text processing and automation.