Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Searching in Text

You know in from lists: "Tom" in guests is True if one of the elements is exactly "Tom". With text, in does more: it checks whether one text is a part of another, anywhere in it.

print("cat" in "concatenate")
print("dog" in "concatenate")
print("An" in ["Anna", "Tom"])
print("An" in "Anna")

This prints:

True
False
False
True

Look at the last two lines. In a list, in looks for a whole element, and no element is "An". In a text, it looks for a part, so it finds "An" at the start of "Anna".

That's how a search box works: it shows the titles that contain what you typed. A search usually doesn't care about case, so each title is turned into lowercase first:

songs = ["Night Drive", "Summer Nights", "Rainy Day", "Midnight Train"]
search = "night"
for song in songs:
    if search in song.lower():
        print(song)

This prints:

Night Drive
Summer Nights
Midnight Train

When you need to know where the part is, use the method find(). It returns the index at which the part starts the first time it appears, or -1 if it isn't in the text at all. And the method count() returns how many times the part appears:

sentence = "The cat sat on the mat."
print(sentence.find("cat"))
print(sentence.find("dog"))
print(sentence.count("at"))
print(sentence.count("the"))

This prints:

4
-1
3
1

"cat" starts at index 4: T is at index 0, and the space at index 3 is a character too. "at" is in the sentence three times, in cat, sat and mat: count() counts parts of the text, not words. And "the" is there only once, because The at the start has a capital T. Like ==, in, find() and count() care about case.

Why -1 and not 0? Index 0 is the start of the text, so 0 means “found right at the start”. When you only need to know whether a part is there, use in: it's simpler than checking find() for -1.

Instructions

  1. 1

    Social networks don't care about case in hashtags: #Python and #python are the same tag. Write an if statement that checks whether post contains tag, in any case, and prints Found: #python. Otherwise, it prints Not found: #python. Take the tag from tag in both, for example in an f-string, and keep the line tag = "#python": the exercise also tries other tags in it.

  2. 2

    Below, print how many hashtags the post has, that is, how many times # appears in it: Hashtags: 3.

Back

5/12

  1. 1. Upper and Lower Case
  2. 2. Trimming Spaces
  3. 3. Replacing Text
  4. 4. Quiz: Text Methods
  5. 5. Searching in Text
  6. 6. Beginnings and Ends
  7. 7. Splitting Text
  8. 8. Joining a List
  9. 9. Slices
  10. 10. Quiz: Slices
  11. 11. Decimal Places
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.