File tree Expand file tree Collapse file tree 4 files changed +10
-10
lines changed
how-to-check-if-a-python-string-contains-a-substring Expand file tree Collapse file tree 4 files changed +10
-10
lines changed Original file line number Diff line number Diff line change 11file_content_title = """Hi There And Welcome.
22This 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- """
3+ I Don't Want To Tell You The Secret,
4+ But I Do Want To Secretly Tell You That I Have One."""
65
76# Strings are case-sensitive
87print ("secret" in file_content_title ) # False
Original file line number Diff line number Diff line change 11raw_file_content = """Hi there and welcome.
22This is a special hidden file with a SECRET secret.
33I don't want to tell you The Secret,
4- but I do want to secretly tell you that I have one.
5- """
4+ but I do want to secretly tell you that I have one."""
65
76# Use the membership operator for pythonic membership checks
87print ("secret" in raw_file_content )
Original file line number Diff line number Diff line change 44file_content = """hi there and welcome.
55this is a special hidden file with a secret secret.
66i don't want to tell you the secret,
7- but i do want to secretly tell you that i have one.
8- """
7+ but i do want to secretly tell you that i have one."""
98
109# Find words that start with "secret"
1110print (re .search (r"secret\w+" , file_content ))
2120# Find all words followed by certain punctuation characters
2221print (re .findall (r"secret[\.,]" , file_content ))
2322
24- # Use a capture group to remove the punctuation character
23+ # Use a capturing group to remove the punctuation character
2524print (re .findall (r"(secret)[\.,]" , file_content ))
2625
2726# Iterate over all matches as Match objects
2827for match in re .finditer (r"(secret)[\.,]" , file_content ):
2928 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 ))
Original file line number Diff line number Diff line change 11file_content = """hi there and welcome.
22this is a special hidden file with a secret secret.
33i don't want to tell you the secret,
4- but i do want to secretly tell you that i have one.
5- """
4+ but i do want to secretly tell you that i have one."""
65
76# Use .index() to get the starting index of the first match
87print (file_content .index ("secret" ))
You can’t perform that action at this time.
0 commit comments