Match string in python regardless of upper and lower case differences [duplicate]

2024/10/13 15:18:15

I'm trying to find a match value from a keyword using python. My values are stored in a list (my_list) and in the below example I'm trying to find the word 'Webcam'. I only want to return the word if it is fully matched.

Using item.find works but only if the case matches (i.e. upper and lower case must be correct). But I want to return the item regardless of the case, However, I do not wish to match all instances of the string like 'Webcamnew' so using the any() method won't work I think. Does anyone know how to do this..?

my_list = ['webcam', 'home', 'Space', 'Maybe later', 'Webcamnew']for item in my_list:if item.find("Webcam") != -1:print item
Answer
my_list = ['webcam', 'home', 'Space', 'Maybe later', 'Webcamnew']for item in my_list:if 'webcam' == item.lower()print item.lower()

Note: Strings are immutable in Python - it doesn't modify the string in the list.

https://en.xdnf.cn/q/69520.html

Related Q&A

Can celery celerybeat use a Database Scheduler without Django?

I have a small infrastructure plan that does not include Django. But, because of my experience with Django, I really like Celery. All I really need is Redis + Celery to make my project. Instead of usin…

Django UserCreationForm custom fields

I am trying to create form for user registration and add some custom fields. For doing that, Ive subclassed UserCretionForm and added fields as shown in django documentation. Then Ive created function-…

Why val_loss and val_acc are not displaying?

When the training starts, in the run window only loss and acc are displayed, the val_loss and val_acc are missing. Only at the end, these values are showed. model.add(Flatten()) model.add(Dense(512, ac…

Is there a python module to solve/integrate a system of stochastic differential equations?

I have a system of stochastic differential equations that I would like to solve. I was hoping that this issue was already address. I am a bit concerned about constructing my own solver because I fear m…

How does thread pooling works, and how to implement it in an async/await env like NodeJS?

I need to run a function int f(int i) with 10_000 parameters and it takes around 1sec to execute due to I/O time. In a language like Python, I can use threads (or async/await, I know, but Ill talk abou…

Calculate centroid of entire GeoDataFrame of points

I would like to import some waypoints/markers from a geojson file. Then determine the centroid of all of the points. My code calculates the centroid of each point not the centroid of all points in the …

Flask-Babel localized strings within js

Im pretty new to both Python and Flask (with Jinja2 as template engine) and I am not sure I am doing it the right way. I am using Flask-Babel extension to add i18n support to my web application. I want…

a (presumably basic) web scraping of http://www.ssa.gov/cgi-bin/popularnames.cgi in urllib

I am very new to Python (and web scraping). Let me ask you a question. Many website actually do not report its specific URLs in Firefox or other browsers. For example, Social Security Admin shows popul…

Why is tuple being returned?

I have the following:tableNumber = session.query(TABLE.TABLESNUMBER).filter_by(TABLESID=self.TABLESID).first() return str(tableNumber)This is my TABLE class:class TABLE(Base):.... TABLESID =…

How to assert both UserWarning and SystemExit in pytest

Assert UserWarning and SystemExit in pytestIn my application I have a function that when provided with wrong argument values will raise a UserWarnings from warnings module and then raises SystemExit fr…