Mike
Mike

Reputation: 4405

Testing String Exists Regardless of Capitalization, Python

I have:

x = []
x.append("ds")

I want to check that "ds" is in x, but I don't care is it is capitalized or not. So if I want to run

if "DS" in x:
    print "Yes"

I want "Yes" to return. I just want to make sure that the string "ds" is in x, regardless of whether or not it is capitalized. How do I do this? I've been through the list of string methods, but I can't seem to find something simple that does this, just a combination of testing different letter cases, which can be cumbersome.

Thanks, Mike

Upvotes: 0

Views: 289

Answers (2)

Brad
Brad

Reputation: 1367

>>> 'DS'.lower()
'ds'

Upvotes: -1

Andrew Clark
Andrew Clark

Reputation: 208505

if any(s.lower() == "ds" for s in x):
    print "Yes"

Of course you could also use s.upper() == "DS".

Upvotes: 4

Related Questions