Tutorials  /  Python

Python Find String in List - Guide

Ccentron Redaktion · February 2024 ·3 min read ·Python, Tutorial

Guide

You are working with Python and want to find out if a specific string is contained in a list? Whether it's searching for keywords, user inputs, or specific data – Python offers several simple and efficient ways to do this.

In this guide, we will walk you through the step-by-step process of checking if a string is present in a list, explore different methods, and help you determine which one best suits your needs. Let's get started! For your information: 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.

Python
l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']

# string in the list
if 'A' in l1:
    print('A is present in the list')

# string not in the list
if 'X' not in l1:
    print('X is not present in the list')

Output:

Code
A is present in the list
X is not present in the list

Let’s look at another example where we will ask the user to enter the string to check in the list.

Python
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:

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

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

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.

Python
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.

Python
l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C']
s = 'A'
matched_indexes = []
i = 0
length = len(l1)

while i < length:
    if s == l1[i]:
        matched_indexes.append(i)
    i += 1

print(f'{s} is present in {l1} at indexes {matched_indexes}')

Output:

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

Register now in the ccenter for exclusive access to advanced tutorials and best practices, or contact our sales team for personalized consulting and tailored solutions! Check out our tutorial: Python: Convert String to List to learn about the different methods for efficiently converting a string into a list, when each method is best suited, and how to avoid common mistakes. This will help you deepen your Python knowledge and optimize your data processing.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren