Python Find String in List | DigitalOcean (2024)

We can use Python in operator to check if a string is present in the list or not. There is also a not in operator to check if a string is not present in the list.

l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']# string in the listif 'A' in l1: print('A is present in the list')# string not in the listif 'X' not in l1: print('X is not present in the list')

Output:

A is present in the listX is not present in the list

Recommended Reading: Python f-strings Let’s look at another example where we will ask the user to enter the string to check in the list.

l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']s = input('Please enter a character A-Z:\n')if s in l1: print(f'{s} is present in the list')else: print(f'{s} is not present in the list')

Output:

Please enter a character A-Z:AA is present in the list

Python Find String in List using count()

We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list.

l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']s = 'A'count = l1.count(s)if count > 0: print(f'{s} is present in the list for {count} times.')

Finding all indexes of a string in the list

There is no built-in function to get the list of all the indexes of a string in the list. Here is a simple program to get the list of all the indexes where the string is present in the list.

l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']s = 'A'matched_indexes = []i = 0length = len(l1)while i < length: if s == l1[i]: matched_indexes.append(i) i += 1print(f'{s} is present in {l1} at indexes {matched_indexes}')

Output: A is present in ['A', 'B', 'C', 'D', 'A', 'A', 'C'] at indexes [0, 4, 5]

You can checkout complete python script and more Python examples from our GitHub Repository.

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python Find String in List | DigitalOcean (2024)
Top Articles
Hurricane Insurance Cost in Florida
Copenhagen ranked among top cities for being happy and healthy  - The Copenhagen Post
Frederick County Craigslist
Bashas Elearning
Breaded Mushrooms
What Happened To Dr Ray On Dr Pol
Chris wragge hi-res stock photography and images - Alamy
Barstool Sports Gif
J Prince Steps Over Takeoff
Geometry Escape Challenge A Answer Key
Clairememory Scam
Cape Cod | P Town beach
Craigslist Heavy Equipment Knoxville Tennessee
All Buttons In Blox Fruits
Willam Belli's Husband
Tyler Sis University City
Air Traffic Control Coolmathgames
Craigslist St. Cloud Minnesota
Craigslist Illinois Springfield
Troy Gamefarm Prices
48 Oz Equals How Many Quarts
Boise Craigslist Cars And Trucks - By Owner
Znamy dalsze plany Magdaleny Fręch. Nie będzie nawet chwili przerwy
Accuradio Unblocked
Trinket Of Advanced Weaponry
Broken Gphone X Tarkov
Tmj4 Weather Milwaukee
Murphy Funeral Home & Florist Inc. Obituaries
Senior Houses For Sale Near Me
Aveda Caramel Toner Formula
Daily Jail Count - Harrison County Sheriff's Office - Mississippi
Busch Gardens Wait Times
WorldAccount | Data Protection
Author's Purpose And Viewpoint In The Dark Game Part 3
Flipper Zero Delivery Time
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Bekkenpijn: oorzaken en symptomen van pijn in het bekken
All Weapon Perks and Status Effects - Conan Exiles | Game...
The Average Amount of Calories in a Poke Bowl | Grubby's Poke
Actress Zazie Crossword Clue
Terrell Buckley Net Worth
Ephesians 4 Niv
Walmart Listings Near Me
FactoryEye | Enabling data-driven smart manufacturing
St Als Elm Clinic
Is TinyZone TV Safe?
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Metra Union Pacific West Schedule
Anthony Weary Obituary Erie Pa
Blippi Park Carlsbad
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5525

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.