In '<string>' requires string as left operand, not list [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
How can I make Python quit if a list contains a particular word?
text = input("Input text: ")
if["exit","quit","close"] in text:
Quit()

It's the other way around
if text in ["exit","quit","close"]:
You can read it as pseudo English, you are checking if the string is in the list.
If you want to check if part of the input is in the list you can use any
if any(x in ["exit", "quit", "close"] for x in text.split(' ')):

You've got it backwards. Try
if text in ["...","..."]:
Think of it as "If this is in that".

Related

How can I write the whole word of the variable, in Python? [duplicate]

This question already has answers here:
Converting a String to a List of Words?
(15 answers)
Split a string with unknown number of spaces as separator in Python [duplicate]
(6 answers)
Closed 2 years ago.
I want to be able to print the whole variable, or just detect it. If I run it through a for loop as I've done in the code below, it just prints the individual letters. How can it print the whole word?
Question = input()
Answer = input()
def test():
for words in Answer:
print(words)
test()
Since answer is a string, and a string is an enumerable type, it enumerates over the characters. If you want to print a plain string just do print(Answer).
This would work perfectly
Question = input()
Answer = input()
def test():
for words in Answer.split(' '):
print(words)
test()
When you are iterating a string in python what it does, it makes an iterable out of a string and then it print's each letter in a given word. This is for both single word and multiple words, so either you can just print(Answer) or you can first split it by e.g. space and then print each word in a sentence like this:
answer = Answer.split() # splits by space, you can provide any separator and this will generate a list for you
for word in answer:
print(word)
This will print each word even if it's only 1.
This is a poorly phrased question and poorly researched question which can get you banned from asking questions in Stack overflow so keep that in mind when you post another question.
You can do
for word in Answer.split(" "):
print(word)
If that's not what you are looking for you can try-
print(Answer)

How to run a statement again and again on a case but on different occurrences [duplicate]

This question already has answers here:
Replace characters in string from dictionary mapping
(2 answers)
Closed 2 years ago.
Hey guys I am making an encoding system in which each letter gets converted into predefined gibberish.
For example, 'a' has already been set as 'ashgahsjahjs'.
But using if a in data: print("ashgahsjahjs") executes this for one time only, if there are more than one A in the word, it would not print them with gibberish.
Using a while loop does not work either as it keeps printing indefinitely, so is there a way to print the gibberish each time there is a new occurrence of a letter.
you could try indexing the string.
your_string = "are you an apple?"
for i in range(len(your_string)):
if "a" == your_string[i]:
print("Found a at position {pos}".format(pos=i))
else:
print("Nope")

python - if statement with logical OR condition [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
How to check if a string contains an element from a list in Python
(8 answers)
Closed 5 years ago.
I'm a beginner in python and trying to achieve the following - need help pls...
In the below code snippet, it matches for ab but not for remaining two matches...Can anyone pls point what I'm missing.
I've a txt input which is captured in text.
for line in text:
parse = re.split('\s+', line)
print(parse)
if ("ab" or "yx" or "12") in line:
print("success")
continue
else:
print("failure")
The above code matches just "ab", it doesn't work for other matches like "yx" and "12". Don't know what I'm missing.
Thanks
I think you mean:
if "ab" in line or "yx" in line or "12" in line:

how would I convert an inputted string into a list? [duplicate]

This question already has answers here:
How to make a list from a raw_input in python? [duplicate]
(4 answers)
Closed 6 years ago.
I am doing a python assessment in school which requires me to take an inputted sentence and a word the user inputs to analyse the text and output the positions of where the word is in the text, for part of this task i am trying to convert the inputted sentence into a list, but i am a bit stuck. any chance of some help?
Are you trying to make a list of the words in the sentence, or of each character?
If the former; then as you did with string.split() is fine.
If the later; try creating a list and then adding each character of the string to it.
This is how I'd do it:
string="Hello World of Python"
char_list=[]
for char in string:
char_list.append(char)

Trying to compare palindrome string in python [duplicate]

This question already has answers here:
How to check for palindrome using Python logic
(35 answers)
Closed 7 years ago.
I'm trying to compare a string and check if it is palindrome or not. I'm using the next method:
name = input("Enter your string")
name1 = name[-1::-1]
if(name==name1):
print("True")
else:
print("False")
but it always shows me False
has anyone idea why its not working properly?
Because you are starting at the last character of the string. You want to use name[::-1] instead. That will take the entire string from beginning to end with a step of -1, meaning that it will be reversed.

Categories