What is the python equivalent of:
if (strpos($elem,"text") !== false) {
// do_something;
}
What is the python equivalent of:
if (strpos($elem,"text") !== false) {
// do_something;
}
pos = haystack.find(needle)
pos = haystack.find(needle, offset)
pos = haystack.index(needle)
pos = haystack.index(needle, offset)
To simply test if a substring is in a string, use:
needle in haystack
which is equivalent to the following PHP:
strpos(haystack, needle) !== FALSE
if needle in haystack:
–
Earl needle in haystack
. Coming from PHP, Python code is so human readable... –
Beaird if elem.find("text") != -1:
do_something
if "this is string example....wow!!!".find("exam") != -1: print "works";
–
Lancer Is python is really pretty that code using "in":
in_word = 'word'
sentence = 'I am a sentence that include word'
if in_word in sentence:
print(sentence + 'include:' + word)
print('%s include:%s' % (sentence, word))
last 2 prints do the same, you choose.
© 2022 - 2024 — McMap. All rights reserved.