Compare string to multiple strings in Python3
I want to know if there is no text string in the variables.
And then compare if there is another text string.
results It may contain the following: yes, no or ?
I have follow code:
internet = result1 vpn = result2 zetas = result3 values = [internet, vpn, zetas] if any(v !== "?" for v in values): print("No exist ?") if any(v == "no" for v in values): print("Exist a NO") else: print("Good")
especially to see the most elegant way of doing this.
you can check
value in list or
value not in list
values = (internet, vpn, zetas) if "?" not in values: print("No exist ?") if "no" in values: print("Exist a NO") else: print("Good")
Comparing Strings using Python, Second, we'll go over both the string and the re modules, which contain methods to python3 comparing-strings.py comparing Berlin with Lausanne: False� $ python3 comparing-strings-re.py Bayswater matches the search pattern Table Bay matches the search pattern Bombay matches the search pattern Multi-Line and List Comparisons. So far our comparisons have only been on a few words. Using the difflib module Python also offers a way to compare multi-line strings, and entire lists of words. The
First it is preferable to pass generator expressions instead of lists for loop feeding.
Secondly, although there can be many ways,, a good way would simply be:
values = (internet, vpn, zetas) if "?" in (v for v in values): print("No exist ?")
Generator expressions save memory and time, not critical for the script in hand but useful for larger chunks of data.
Stop Using “or” to Test Multiple Values Against a Variable in Python , Use the “in” keyword for multiple comparisons. Jonathan Hsu Here's a simple example, checking for the letter e in a string: result = 'e' in 'The' String Comparison with == and != Operator You can easily compare two Strings and find out whether the two Strings are equal or not, with the help of Equal to(==) and Not Equal to(!=) Operator in Python. Let’s see with an Example in which we are taking string value in a country variable. Then we are comparing the strings with == and != Operator.
I checked if the query you had regarding the question mark in inverted commas would escape detection using an array comparison
[x in B for x in A]
#I assigned values to your variables to test result1 ='?' result2="no" result3 = "yes" internet = result1 vpn = result2 zetas = result3 #created an array with above values in it both in string format and as variable values (appended a number just for test) A = [internet, 'no', '?', 'yes', vpn, zetas, '8'] #array of sample strings B = ['?', 'no', 'yes'] exists = [x in B for x in A] print (exists)
When the results of the comparison printed it gave: [True, True, True, True, True, True, False]. However if there were additional quote marks, e.g.
'"?"' , the comparison on this printed as False. (Items in single quotes evaluate the same as double quotes..)
Feel free to run/re-assign values/test etc as you please. (might not be the answer you're looking for but hope it helps)
How to check if multiple strings exist in another string in Python?, To check if any of the strings in an array exists in another string, you 'o', 'u'] str = "hello people" if any(c in str for c in arr): print "Found a match"� Compare Strings by Comparison Operators. Fun fact: we don’t have to write our own string equality code to compare strings. As it turns out, there are several core operators that work with strings right out of the box: ==, <, <=, >=, >. Using our Penguins players from above, we can try comparing them directly:
4 Ways of Python String Comparison with 5 Examples, If you simply require comparing the values of two variables then you may use the '==' operator. If strings are same, it evaluates as True, otherwise False. Have a� #!/usr/bin/python3 para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up
Python, This topic explains how to handle strings in Python and provides examples of operations that deal The quotation characters are allowed within a string if they do not match the enclosing pair. Replaces tabs in a string with multiple spaces. If you use two identical strings, instead of wasting memory by creating two string objects, all interned strings with the same contents point to the same memory. This results in the Python "is" operator returning True because two strings with the same contents are pointing at the same string object. This will also happen in Java and in C.
Storing Multiple Values in Lists – Programming with Python, This does not mean that variables with string or number values are constants, but when we This is because Python stores a list in memory, and then can use multiple names to Use a for-loop to convert the string “hello” into a list of letters: to create a new string that contains only every other character in the string “In an� Seems question is not about strings equality, but of sets equality. You can compare them this way only by splitting strings and converting them to sets: s1 = 'abc def ghi' s2 = 'def ghi abc' set1 = set(s1.split(' ')) set2 = set(s2.split(' ')) print set1 == set2 Result will be. True
- Can you please use some examples to demonstrate what you're trying to do and what the expected output is?
- It looks like you are looking for elif. And also, there is no
- instead of
(v for v in values)you can use
if "?" in values:
- That's right! But this way it will prevent "?" inside strings from escaping detection, I thought it would be a nice feature!
(v for v in values)and
valueswill work the same so there is no feature.
- That's actually not correct. But you can try it.
python values = ["qwerty", "asdf"] if "qwert" in values: print('1') if "qwert" in (v for v in values): print('2')You will see that it will print 2.
- My python print nothing for both versions. And this is correct result because there is no
["qwerty", "asdf"]. If I use
if "qwerty"in both versions then I see