0

I am wanting to compare a string called itemToReplace to one in an array but ignoring the case (if it's a capital letter or lower case).

Here is the code:

itemToReplace = input("Choose an item to replace: ")
if itemToReplace in self._inventory:
    # do something...

And when I do this, if the user types in something like "sWord" instead of "Sword", it won't work. So does anyone know how to do this?

Thanks.

pault
  • 41,343
  • 15
  • 107
  • 149
Kieran
  • 41
  • 1
  • 2
  • 9
  • simply add either `.upper()` or `.lower()` to the end of the input and it will change the case so you can deal with it easier – johnashu Mar 09 '18 at 15:01
  • Do you want to ignore the case and other extra capitalized letters in the string? If so, then use .lower() method on the string you want. – R.R.C. Mar 09 '18 at 15:04

2 Answers2

0

Change the case of the entire input by doing.

itemToReplace = input("Choose an item to replace: ").upper() #Uppercase

or

itemToReplace = input("Choose an item to replace: ").lower() #Lowercase
johnashu
  • 2,167
  • 4
  • 19
  • 44
0

This should help.

import string
print 'sWord'.lower() in map(string.lower, ['Sword'])

Output:

True
  1. Convert the input to lower case
  2. use map method to convert all string elements in list to lower and the use in .
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • he needs to change the case of a single string input, not a list.. Ideal for a list but a little too much for a single string..of course if the `_inventory` is full of mixed case strings then this will be ideal! – johnashu Mar 09 '18 at 15:09