Skip to content

Commit 6e796b5

Browse files
committed
Add materials code
1 parent b5a27c1 commit 6e796b5

File tree

7 files changed

+1108
-0
lines changed

7 files changed

+1108
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# How to Check if a Python String Contains a Substring
2+
3+
If you're new to programming or come from a different programming language to Python, you may not know what's the best way to check whether a string contains another string in Python.
4+
5+
In this tutorial you'll focus on the most pythonic approach to tackle this task, using the **membership operator `in`**. Additionally, you'll learn how to **identify the right string methods** for related, but different, use cases.
6+
7+
Finally, you'll also learn how to **find substrings in pandas columns**. This is helpful if you need to search through data from a CSV file.
8+
9+
Head over to Real Python to the full tutorial on [How to Check if a Python String Contains a Substring](https://realpython.com/python-string-contains-substring/).
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
file_content_title = """Hi There And Welcome.
2+
This Is A Special Hidden File With A Secret Secret.
3+
I Don'T Want To Tell You The Secret,
4+
But I Do Want To Secretly Tell You That I Have One.
5+
"""
6+
7+
# Strings are case-sensitive
8+
print("secret" in file_content_title) # False
9+
10+
# Convert the input string to lowercase to generalize matching
11+
file_content = file_content_title.lower()
12+
13+
print(file_content)
14+
15+
print("secret" in file_content) # True
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
raw_file_content = """Hi there and welcome.
2+
This is a special hidden file with a SECRET secret.
3+
I don't want to tell you The Secret,
4+
but I do want to secretly tell you that I have one.
5+
"""
6+
7+
# Use the membership operator for pythonic membership checks
8+
print("secret" in raw_file_content)
9+
10+
# You can do the inverse with `not in`
11+
print("secret" not in raw_file_content)
12+
13+
# Using `in` is a readable way to check if a string contains a substring
14+
if "secret" in raw_file_content:
15+
print("Found!")

how-to-check-if-a-python-string-contains-a-substring/companies.csv

Lines changed: 1001 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pandas as pd
2+
3+
4+
# Read the CSV data into a pandas dataframe
5+
companies = pd.read_csv("companies.csv")
6+
7+
# Check the size of the dataframe
8+
print(companies.shape)
9+
10+
# Inspect the first 5 rows of the dataframe
11+
print(companies.head())
12+
13+
# Filter for rows where the company slogan contains the word "secret"
14+
print(companies[companies.slogan.str.contains("secret")])
15+
16+
# Apply a regex pattern to the string search to narrow the results
17+
print(companies[companies.slogan.str.contains(r"secret\w+")])
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import re
2+
3+
4+
file_content = """hi there and welcome.
5+
this is a special hidden file with a secret secret.
6+
i don't want to tell you the secret,
7+
but i do want to secretly tell you that i have one.
8+
"""
9+
10+
# Find words that start with "secret"
11+
print(re.search(r"secret\w+", file_content))
12+
13+
# Inspect what methods on the Match object can give you
14+
m = re.search(r"secret\w+", file_content)
15+
print(m.group()) # "secretly"
16+
print(m.span()) # (128, 136)
17+
18+
# Find the first word followed by certain punctuation characters
19+
print(re.search(r"secret[\.,]", file_content))
20+
21+
# Find all words followed by certain punctuation characters
22+
print(re.findall(r"secret[\.,]", file_content))
23+
24+
# Use a capture group to remove the punctuation character
25+
print(re.findall(r"(secret)[\.,]", file_content))
26+
27+
# Iterate over all matches as Match objects
28+
for match in re.finditer(r"(secret)[\.,]", file_content):
29+
print(match)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
file_content = """hi there and welcome.
2+
this is a special hidden file with a secret secret.
3+
i don't want to tell you the secret,
4+
but i do want to secretly tell you that i have one.
5+
"""
6+
7+
# Use .index() to get the starting index of the first match
8+
print(file_content.index("secret"))
9+
10+
# Pass a custom start index to .index() to search from a different point
11+
print(file_content.index("secret", 60))
12+
13+
# Count the number of matches in the text
14+
print(file_content.count("secret"))
15+
16+
# Use a for loop to inspect all matches
17+
for word in file_content.split():
18+
if "secret" in word:
19+
print(word)
20+
21+
# Use a list comprehension and a conditional check for a one-liner
22+
print([word for word in file_content.split() if "secret" in word])

0 commit comments

Comments
 (0)