I am splitting a string "name:john"
and want to check whether the split happened or not. What is the right way doing that check?
A quick solution: (but maybe overkill)
name = "name:john"
splitted = name.split(":")
if len(splitted) > 1:
print "split"
Is there a more elaborate way of doing the check?
You can also choose the EAFP approach: split, unpack and handle ValueError
:
try:
key, value = name.split(":")
except ValueError:
print "Failure"
else:
print "Success"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With