Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Strings and Ranges

Strings

Strings can be found and used everywhere in your Python code. Some example string manipulation functions are shown below.

Manipulating Strings

Output
Output
['palay', '&', 'corn']
Output
palay
&
corn
Output
'palay'
Output
['palay', 'corn']

In all the string operations above, the result is a list.

We can also check if a string contains a substring using in.

Output
True
Output
0

Formatting Strings

Output
'PALAY & CORN'
Output
'palay & corn'
Output
'Palay & Corn'

You can also use the % operator, the format method, or f-strings (new in Python 3) to format strings.

Output
'palay & corn'
Output
'palay & corn'
Output
'palay & corn'
Output
'palay & CORN'

Ranges

Ranges are very useful if you need a list of intergers in a for loop.

Output
[0, 1, 2, 3, 4]

Adding a third parameter to the range function specifies the step (default: 1)

Output
[1, 4, 7, 10, 13]
Output
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]

Regular Expressions (regex)

Regular expressions (regex) in Python are used for matching text patterns and are implemented through the re module that allows you to search, match, and manipulate strings using specific patterns defined by regex syntax.

What can Regex do?

  • Search: Find specific patterns within text

  • Validate: Ensure that text conforms to certain rules or formats

  • Extract: Pull out specific parts of text, such as phone numbers or email addresses

Basic Regex Concepts

  1. Patterns: A regex pattern is a sequence of characters that describes the structure of the text you want to match.

  2. Characters: In regex, characters have special meanings and can be used to create patterns. For example:

    • . matches any single character (except newline)

    • * matches zero or more occurrences of the preceding pattern

    • + matches one or more occurrences of the preceding pattern

  3. Character Classes: Regex provides several pre-defined character classes that match specific sets of characters, such as:

    • \d matches any digit (0-9)

    • \w matches any word character (alphanumeric plus underscore)

    • \s matches any whitespace character

Basic Regex Syntax

  1. Literal Characters: Match a single character exactly

    • a matches the letter “a” exactly

  2. Character Classes: Use special characters to match specific sets of characters

    • [abc] matches any of the characters “a”, “b”, or “c”

  3. Quantifiers: Control how many times a pattern is matched

    • a* matches zero or more occurrences of the character “a”

  4. Groups and Captures: Enclose a sub-pattern in parentheses to create a group and capture its value

Example Regex Pattern

Let’s say we want to match any phone number that follows this format: (123) 456-7890. We can write a regex pattern like this:

\(\d{3}\) \d{3}-\d{4}

Here, \( matches the opening parenthesis, \d{3} matches exactly three digits, ) matches the closing parenthesis, and so on.

Problem: Write a function that takes a string as input and returns the first word that starts with an uppercase letter.

Source
The first capitalized word in is Hello THIS test a world is: Hello
The first capitalized word in is world Hello a test THIS is: Hello
The first capitalized word in world is Hello test a THIS is: Hello
The first capitalized word in THIS a world test is Hello is: THIS
The first capitalized word in is test a THIS Hello world is: THIS

Practice Exercises

Extract correct phone numbers

Task: We define a correct phone number is one that starts with +639 or 639 and contains 12 digits. Extract correct and incorrect phone numbers from a list.

Source
Correct Phone Numbers: ['+639123456789', '639987654321']
Incorrect Phone Numbers: ['+63987654320', '01234567890', '+63998876543241']