1
def can_spell_with(target_word, letter_word):
    valid = True
    target_word1 = [x.lower() for x in target_word]
    letter_word1 = [x.lower() for x in letter_word]
    for c in target_word1:
        if c not in letter_word1:
            valid = False
        else:
            valid = True
    return valid
print(can_spell_with('elL','HEllo'))
# True
print(can_spell_with('ell','helo'))
# False

In the code above: I am trying to figure out how to return True if the letter_word contains the target_word.

So 'ell' in 'helo' would return False But 'ell' in 'hello' would return True

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Gojilla
  • 81
  • 1
  • 7
  • does character case matter, upper case or lower case ? – sahasrara62 Jun 10 '20 at 05:13
  • Does this answer your question? [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – sahasrara62 Jun 10 '20 at 05:27
  • capitalization does not matter and thank you for the suggestion, there is quite some details in the explanations there thank you – Gojilla Jun 10 '20 at 09:23

5 Answers5

2

Try to use in:

def can_spell_with(a, b):
     return (a.upper() in b.upper())
print(can_spell_with('Ell','HEllo'))
>>>True
print(can_spell_with('ell','helo'))
>>>False
MrNobody33
  • 6,413
  • 7
  • 19
2

You can use in but you first have to make the cases the same:

def can_spell_with(target_word, letter_word):
    return target_word.lower() in letter_word.lower()
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
2

You're doing a case-insensitive search. No need to do a list comprehension. Just use lower() or upper() to convert all characters to lower or uppercase and use in:

def can_spell_with(target_word, letter_word):
    return target_word.lower() in letter_word.lower()

Alternatively you could do a case insensitive regex search:

import re

def can_spell_with(target_word, letter_word):
    return re.search(target_word, letter_word, re.IGNORECASE) is not None
jignatius
  • 6,304
  • 2
  • 15
  • 30
1
target_word = target_word.lower() 
letter_word = letter_word.lower()

lenn = len(target_word)     
valid = False               

for i in range(len(letter_word)-lenn):   
    if letter_word[i:i+lenn] == target_word:   
        valid = True
return valid
Saurabh Mahra
  • 330
  • 5
  • 10
  • How does this part work: for i in range(len(letter_word)-lenn): if letter_word[i:i+lenn] == target_word: how does it know for 'ell' and 'helo' that the second 'l' is not there in 'helo'? to me it looks like it searches for e in helo then l in helo then l in helo which works out to be true – Gojilla Jun 10 '20 at 08:16
1

this will check the whether word1 in word2 irrespective of lowercase or uppercase

def func(word1, word2):
    first_char = word1[0]
    for index, char in enumerate(word2):
        if char==first_char:
            if word2[index:len(word1)+1]==word1:
                return True
    return False
sahasrara62
  • 10,069
  • 3
  • 29
  • 44