I want to find Andy in the list. Either Andy comes in upper or lower case in string the program should return True
String1 ='Andy is an awesome bowler'
String2 ='andy is an awesome bowler'
String3 ='ANDY is an awesome bowler'
I am doing in this way
if 'Andy' in String1:
print ('success')
else:
print("Nothing")
But it is working only for Andy
not for andy
or ANDY
. I do not want to use Pattern
matching. By using lower()
the Andy
can be converted to lower case but I need both at a time ignoring lower as well as upper case. Is it possible?
You can convert the string to uppercase or lowercase first:
String1 ='Andy is an awesome bowler'
String2 ='andy is an awesome bowler'
String3 ='ANDY is an awesome bowler'
if 'andy' in String1.lower(): # or this could be if 'ANDY' in String1.upper():
print('success')
else:
print('Nothing')
If you are sure that you string contains only ascii characters, use the following approach:
if 'andy' in string1.lower():
do something
If your comparision strings involve unicodedata:
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("any_string", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
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.