skip to Main Content

I’m learning how to use the Twitter API using tweepy. I’ve produced a list of recent tweets with the keyword "tea garden" . From this list I want to search for strings in the tweets that include all capitalization variants of those strings.

My initial list of tweets w/ keywords

public_tweets = api.search("tea garden", count=200)

What do I add in this code to search for all upper/lowercase variants of the string? In this example, it would add tweets to the list that contained "Garden Tea" or "GARDEN TEA".

list = []

for tweet in public_tweets:
  if "garden tea" in tweet.text:
    list.append(tweet)

2

Answers


  1. you can use str.lower to convert it to lowercase first, then compare:

    list = []
    
    for tweet in public_tweets:
      if "garden tea" in tweet.text.lower():
        list.append(tweet)
    
    Login or Signup to reply.
  2. you can use str.upper to convert it to uppercase first, then compare:

    list = []
    
    for tweet in public_tweets:
      if "GARDEN TEA" in tweet.text.upper():
        list.append(tweet)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search