Multiple strings in one variable
Question:
Say, for example, I have some code that goes like this:
sentence = input("Enter input: ")
target_letter = "a" or "b" or "c"
print(sentence.index(target_letter))
My main aim is to get the program to print any of the target letters’ minimum index in the sentence. However when the Boolean "or" operators are used here it only recognizes the first string in the variable. For example: If I type
Ice cream
The program should recognize the ‘c’ as the first valid string in the index and print "1" but it skips to ‘a’ and prints "7".
The same sort of problem arises with the "and" operator:
sentence = input("Enter input: ")
target_letter = "a" and "b" and "c"
print(sentence.index(target_letter))
This time the program only recognizes the last string in the variable instead, if I type:
Exactly
The program throws back "3" becuase c’s index is 3 but the first letter is actually ‘a’ with index 2. And when the input had only a "b" in it out of the strings, the program threw an error because it was neither the first string or the last string.
How would this problem be fixed so that I could place multiple possible strings in a variable so that one of the strings in the variable would be recognized later in the program?
Answers:
One way to do this is with a regular expression that matches any of those characters.
import re
pattern = r'[abc]' # define regex that matches 'a' OR 'b' OR 'c'
sentence = 'Exactly'
m = re.search(pattern, sentence)
if m:
print(m.start())
Based on what I understood from your description, I guess, this should help.
sentence = input("Enter input: ")
for i in sentence:
if i in "abc":
print(i, " : ", sentence.index(i))
Let’s examine the target_letter = "a" or "b" or "c" first
.
>>> target_letter = "a" or "b" or "c"
>>> target_letter # will always return 'a' because 'a' is true.
'a'
>>> # The only string which will return false is the empty string '' or "".
A proposed solution
sentence = input("Enter input: ")
target_letters = ["a", "b", "c"]
for letter in sentence:
if letter in target_letters:
print(letter, 'was found at location in ',sentence.index(letter))
else:
print(letter, "was not found in sentence,", sentence)
Output
Enter input: Ice creeam
I was not found in sentence, Ice creeam
c was found at location in 1
e was not found in sentence, Ice creeam
was not found in sentence, Ice creeam
c was found at location in 1
r was not found in sentence, Ice creeam
e was not found in sentence, Ice creeam
e was not found in sentence, Ice creeam
a was found at location in 8
m was not found in sentence, Ice creeam
Process finished with exit code 0
We can achieve this using next
and a generator expression along with enumerate
.
>>> s = "ice cream"
>>> next((i for i, ch in enumerate(s) if ch in "abc"), None)
1
enumerate
gives us the index as we iterate over the string. The generator returns indices where the corresponding character is in "abc"
. Then we call next
to get the first match or None
is a match is not found.
Say, for example, I have some code that goes like this:
sentence = input("Enter input: ")
target_letter = "a" or "b" or "c"
print(sentence.index(target_letter))
My main aim is to get the program to print any of the target letters’ minimum index in the sentence. However when the Boolean "or" operators are used here it only recognizes the first string in the variable. For example: If I type
Ice cream
The program should recognize the ‘c’ as the first valid string in the index and print "1" but it skips to ‘a’ and prints "7".
The same sort of problem arises with the "and" operator:
sentence = input("Enter input: ")
target_letter = "a" and "b" and "c"
print(sentence.index(target_letter))
This time the program only recognizes the last string in the variable instead, if I type:
Exactly
The program throws back "3" becuase c’s index is 3 but the first letter is actually ‘a’ with index 2. And when the input had only a "b" in it out of the strings, the program threw an error because it was neither the first string or the last string.
How would this problem be fixed so that I could place multiple possible strings in a variable so that one of the strings in the variable would be recognized later in the program?
One way to do this is with a regular expression that matches any of those characters.
import re
pattern = r'[abc]' # define regex that matches 'a' OR 'b' OR 'c'
sentence = 'Exactly'
m = re.search(pattern, sentence)
if m:
print(m.start())
Based on what I understood from your description, I guess, this should help.
sentence = input("Enter input: ")
for i in sentence:
if i in "abc":
print(i, " : ", sentence.index(i))
Let’s examine the target_letter = "a" or "b" or "c" first
.
>>> target_letter = "a" or "b" or "c"
>>> target_letter # will always return 'a' because 'a' is true.
'a'
>>> # The only string which will return false is the empty string '' or "".
A proposed solution
sentence = input("Enter input: ")
target_letters = ["a", "b", "c"]
for letter in sentence:
if letter in target_letters:
print(letter, 'was found at location in ',sentence.index(letter))
else:
print(letter, "was not found in sentence,", sentence)
Output
Enter input: Ice creeam
I was not found in sentence, Ice creeam
c was found at location in 1
e was not found in sentence, Ice creeam
was not found in sentence, Ice creeam
c was found at location in 1
r was not found in sentence, Ice creeam
e was not found in sentence, Ice creeam
e was not found in sentence, Ice creeam
a was found at location in 8
m was not found in sentence, Ice creeam
Process finished with exit code 0
We can achieve this using next
and a generator expression along with enumerate
.
>>> s = "ice cream"
>>> next((i for i, ch in enumerate(s) if ch in "abc"), None)
1
enumerate
gives us the index as we iterate over the string. The generator returns indices where the corresponding character is in "abc"
. Then we call next
to get the first match or None
is a match is not found.