1

Possible Duplicate:
How do I do a case insensitive string comparison in Python?

I wanted to know that if there is any function similar to strcmpi or some provision for it.

Suppose if user enters word harry potter and my file has this word written in this form Harry Potter, so how can I compare it because it simply ignores this because of the upper and lower case letters .

What if I want to convert an "instance" type object to str . How can I do that ?

Community
  • 1
  • 1
POOJA GUPTA
  • 2,295
  • 7
  • 32
  • 60

3 Answers3

4

One simple way is to use .lower() on both strings and then just compare for equality:

>>> "Harry Potter".lower() == "harry potter".lower()
True
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
2

You may convert both string to lower case before comparison:

a = "Harry Potter"
b = "harry potter"

print a.lower()==b.lower()
Howard
  • 38,639
  • 9
  • 64
  • 83
0

You can also use string.find

string.find(s, sub[, start[, end]])

To find all string ignoring the case:

str = "Hello! How are you?"
str.lower().find('hello'.lower())
Zubair Afzal
  • 2,016
  • 20
  • 29