Skip to content

Week 8 - Strings & Regular Expressions

Strings and Regular Expressions


Lesson Objectives

  • Understand strings as sequences and practice accessing individual characters
  • Learn string slicing and manipulation techniques
  • Recognize the immutability of strings and explore methods for creating modified strings
  • Utilize string comparison operations for alphabetic ordering
  • Implement essential string methods and apply them in reading and writing files
  • Gain familiarity with regular expressions to search, replace, and validate patterns within text

1. Review of Strings are Sequences

Explanation:
Strings in Python are sequences (specifically a sequence of characters strung together), which means they can be indexed to access individual characters. Unlike integers or booleans, strings can hold multiple characters in a particular order.

Code Example:

1
2
3
4
5
6
7
8
9
city = "Eureka"
first_letter = city[0]  # Accessing first character
second_letter = city[1]  # Accessing second character
print("First letter:", first_letter)
print("Second letter:", second_letter)

# Negative index
last_letter = city[-1]
print("Last letter:", last_letter)

NOTE: The [-1] syntax is specific to interpreted languages like Python.


2. String Slicing

Explanation:
String slicing enables access to parts of a string. The syntax string[start:end] captures a substring from the starting index up to, but not including, the ending index.

Code Example:

1
2
3
4
sentence = "Programming is fun!"
print("First 11 characters:", sentence[:11])
print("Characters from index 5 to 9:", sentence[5:9])
print("Last 3 characters:", sentence[-3:])


3. String Immutability

Explanation:
Strings are immutable, meaning that their characters cannot be changed directly. Instead, we create a new string by modifying or combining parts of the original.

Code Example:

1
2
3
4
5
6
7
greeting = "Goodbye"
# Attempting to change the first letter results in an error
greeting[0] = 'H'

# So the correct way is to create a new string using slicing.
new_greeting = "H" + greeting[1:]
print("New greeting:", new_greeting)


4. Review of String Comparison

Explanation:
Strings can be compared using relational operators such as <, >, and ==. Python uses ASCII values for comparison, which places uppercase letters before lowercase letters.

Code Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def compare_to_word(word):
    if word < "apple":
        print(f"{word} comes before apple.")
    elif word > "apple":
        print(f"{word} comes after apple.")
    else:
        print("It's apple!")

compare_to_word("banana")
compare_to_word("Apple")


5. Review of String Methods

Explanation:
Strings come with various built-in methods, including upper(), lower(), find(), and replace(), which allow transformations and searches within strings.

Code Example:

1
2
3
4
5
message = "Hello, World!"
print("Uppercase:", message.upper())
print("Lowercase:", message.lower())
print("Position of 'World':", message.find("World"))
print("Replacing 'World' with 'Universe':", message.replace("World", "Universe"))


6. Reading and Writing Files

Explanation:
Reading and writing text files involves opening a file, processing its content, and saving any modifications. We often use loops to handle each line of the file.

Code Example:

1
2
3
4
5
6
7
8
# Writing to a file
with open("greeting.txt", "w") as writer:
    writer.write("Hello, World!\nWelcome to Python programming.")

# Reading from a file
with open("greeting.txt", "r") as reader:
    for line in reader:
        print(line.strip())


7. Introduction to Regular Expressions

Explanation:
Regular expressions (regex) are used to identify specific patterns in strings. They enable powerful operations like search and replace for advanced text processing.

Code Example:

1
2
3
4
5
6
7
8
9
import re

text = "Welcome to the world of Python programming!"
pattern = r"\bworld\b"  # Matches 'world' as a whole word \b is a word boundary
match = re.search(pattern, text)
if match:
    print("Found 'world' at index:", match.start())
else:
    print("'world' not found")


8. String Substitution with Regex

Explanation:
String substitution replaces parts of a string based on a pattern. Regex substitutions use the sub() method to replace matches with a specified replacement.

Code Example:

1
2
3
4
text = "The color of the sky is colorless."
pattern = r"\bcolor\b"
new_text = re.sub(pattern, "colour", text)
print("Modified text:", new_text)


Exercises

  1. Slice and Reorder:
    Write a function half_mirror(str_in) that takes a string input and creates a mirrored version of its first half. If the string has an odd number of characters, consider only the first half (ignoring the middle character). Return the mirrored half as a new string. Example: Input: “PythonProgramming”, Output: “rPnohtyP”

  2. Find and Count Patterns:
    Create a function count_pattern_occurrences(text, pattern) that counts how many times a specified pattern appears in a given text, using regular expressions.
    Example: For pattern = r"\bthe\b", text = "The cat chased the mouse." should return 2.

  3. File Content Replacer:
    Write a program that reads from a file and replaces all occurrences of a user-specified word with another word. Save the modified text to a new file.
    Example: Replace "sky" with "ocean" in a file named nature.txt and save the results in new_nature.txt.