-1

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?

Humty
  • 1,321
  • 3
  • 18
  • 35
  • 1
    Possible duplicate of [How to convert string to lowercase in Python?](http://stackoverflow.com/questions/6797984/how-to-convert-string-to-lowercase-in-python) – idjaw Sep 19 '16 at 12:43
  • user regex..... – Avinash Raj Sep 19 '16 at 12:43
  • 3
    Why not just lower the entire string and see if `andy` is in the string? – idjaw Sep 19 '16 at 12:44
  • 1
    code form of idjaw's comment `if 'andy' in str.lower():` – Avinash Raj Sep 19 '16 at 12:45
  • Read [this](https://docs.python.org/3/library/stdtypes.html#str.lower) – idjaw Sep 19 '16 at 12:45
  • @idjaw and Avinash Raj . I have a lot of data in the form of strings. It will not better to lower the whole string first and then match. – Humty Sep 19 '16 at 12:46
  • 3
    @Humty did you time things to see what would in fact be faster to use? If you are dealing with large amounts of data, have you thought of using something that is designed to deal with large amounts of data? If you have performed these analysis already it would imperative and helpful to the reader to include all your research so you avoid getting answers you believe are not helpful to your solution. – idjaw Sep 19 '16 at 12:49
  • 3
    @AvinashRaj This question is off-topic on Programmers - we do not accept questions about writing or debugging code. Please do not suggest that people take their questions to other communities, especially when the question is not a good fit there. It's a poor user experience when questions are down voted, closed, and deleted, and it's even worse when the automated blocks activate and they are prevented from contributing to the community. – Thomas Owens Sep 19 '16 at 12:56
  • 1
    @AvinashRaj this question is a poor fit for Programmers - it would be quickly voted down and closed over there, see [Why are implementation and debugging questions unwelcome on Programmers.SE?](http://meta.programmers.stackexchange.com/q/7864/31260) – gnat Sep 19 '16 at 12:56
  • 2
    @Humty You deny the solution of lowering both strings and comparing afterwards. But there's no better (more efficient) way of doing it. I think you believe there's some "under the hood" connection between matching capital and normal case letters. But as a matter of fact, only methods like `.lower()` and `upper()` know the mapping between for example "A" and "a". There is no other way to achieve case insensitive comparison than bringing both strings/chars to the same case. – ElmoVanKielmo Sep 19 '16 at 13:07
  • @ElmoVanKielmo Thanks dear. – Humty Sep 19 '16 at 13:08

2 Answers2

3

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')
Farhan.K
  • 3,425
  • 2
  • 15
  • 26
  • K I have a lot of data in the form of strings. It will not better to lower the whole string first and then match. – Humty Sep 19 '16 at 12:47
  • @Farhan.K You should probably add a 'andy'.lower() to clarify that the **transformation has to be done on both strings for the solution to be truly case insensitive** as stated in the question and in the example. – jadsq Sep 19 '16 at 15:58
1

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)
Jaydev
  • 1,794
  • 20
  • 37