Skip to content

Commit 0f3c923

Browse files
authored
Merge branch 'master' into python-exec
2 parents f0b9507 + 2538933 commit 0f3c923

File tree

19 files changed

+1463
-0
lines changed

19 files changed

+1463
-0
lines changed
108 KB
Binary file not shown.

github-copilot/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GitHub Copilot: Fly With Python at the Speed of Thought
2+
3+
Supplementing materials for the [tutorial on GitHub Copilot](https://realpython.com/github-copilot-python/) hosted on Real Python.
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
# Strings are case-sensitive
7+
print("secret" in file_content_title) # False
8+
9+
# Convert the input string to lowercase to generalize matching
10+
file_content = file_content_title.lower()
11+
12+
print(file_content)
13+
14+
print("secret" in file_content) # True
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
# Use the membership operator for pythonic membership checks
7+
print("secret" in raw_file_content)
8+
9+
# You can do the inverse with `not in`
10+
print("secret" not in raw_file_content)
11+
12+
# Using `in` is a readable way to check if a string contains a substring
13+
if "secret" in raw_file_content:
14+
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
# Find words that start with "secret"
10+
print(re.search(r"secret\w+", file_content))
11+
12+
# Inspect what methods on the Match object can give you
13+
m = re.search(r"secret\w+", file_content)
14+
print(m.group()) # "secretly"
15+
print(m.span()) # (128, 136)
16+
17+
# Find the first word followed by certain punctuation characters
18+
print(re.search(r"secret[\.,]", file_content))
19+
20+
# Find all words followed by certain punctuation characters
21+
print(re.findall(r"secret[\.,]", file_content))
22+
23+
# Use a capturing group to remove the punctuation character
24+
print(re.findall(r"(secret)[\.,]", file_content))
25+
26+
# Iterate over all matches as Match objects
27+
for match in re.finditer(r"(secret)[\.,]", file_content):
28+
print(match)
29+
30+
# Print only the first capturing group from the match
31+
for match in re.finditer(r"(secret)[\.,]", file_content):
32+
print(match.group(1))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
# Use .index() to get the starting index of the first match
7+
print(file_content.index("secret"))
8+
9+
# Pass a custom start index to .index() to search from a different point
10+
print(file_content.index("secret", 60))
11+
12+
# Count the number of matches in the text
13+
print(file_content.count("secret"))
14+
15+
# Use a for loop to inspect all matches
16+
for word in file_content.split():
17+
if "secret" in word:
18+
print(word)
19+
20+
# Use a list comprehension and a conditional check for a one-liner
21+
print([word for word in file_content.split() if "secret" in word])

python-absolute-value/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Find an Absolute Value in Python
2+
3+
Supplemental materials for the [tutorial on absolute values](https://realpython.com/python-absolute-value/) hosted on Real Python.

0 commit comments

Comments
 (0)