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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
Exercises¶
-
Slice and Reorder:
Write a functionhalf_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” -
Find and Count Patterns:
Create a functioncount_pattern_occurrences(text, pattern)that counts how many times a specified pattern appears in a given text, using regular expressions.
Example: Forpattern = r"\bthe\b",text = "The cat chased the mouse."should return2. -
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 namednature.txtand save the results innew_nature.txt.