I'm writing a function in Python that should search a string for a substring, then assign the index of the substring to a variable. And if the substring isn't found, I'd like to assign -1 to the variable to be used as a stop code elsewhere. But I get an error that I don't understand. Here is a simplified version of the code:
test = "abc"
search_str = "z"
index_search_str = test.index(search_str) if search_str in test else index_search_str = -1
If I run this code, the value of index_search_str should be -1, but instead I get this error (using PyCharm):
index_search_str = test.index(search_str) if search_str in test else index_search_str = -1
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
But if I change = -1 to := -1, it still gives an error. What am I missing?
index_search_str = test.index(search_str) if search_str in test else -1index_search_str = test.find(search_str)?