Is there a way to account for both upper and lower case letter in python? Here's the example:
if 'jay' in rapper:
print 'blah blah blah'
I want the if statement to be true for Jay or jay.
What can I do?
if 'jay' in rapper.lower():
#do stuff
Just to offer an alternative (@JoelCornett's solution is nicer) you could also do:
if rapper in ('jay', 'Jay'):
# do stuff
An advantage this approach has is that you could check for different names (though not a requirement in this case).
I am assuming that rapper
is a string in the absence of any other information.
if rapper is a list of strings
if "jay" in [x.lower() for x in rapper]:
# do something
print "done"
The simplest way would be to do this (assuming rapper
is a non-null string):
if rapper.strip().lower() == 'jay':
print 'blah blah blah'
Another option, using regular expressions:
import re
if re.match(r'Jay', rapper.strip(), re.IGNORECASE):
print 'blah blah blah'
The above will work for rapper =
'jay'
or 'jAy'
or 'JAY'
or ' Jay '
... etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.