I split the string "name:john" and want to check if the split has occurred or not. What is the correct way to check?
"name:john"
Quick fix: (but maybe brute force)
name = "name:john" splitted = name.split(":") if len(splitted) > 1: print "split"
Is there a more sophisticated way to check?
You can also select EAFP : split, unzip and handle a ValueError :
ValueError
try: key, value = name.split(":") except ValueError: print "Failure" else: print "Success"
Why not use the in operand?
if ':' in name: print "split"
Or, if you want to : preset between the last and final character, you can do:
:
if ':' in name[1:-1]: print "split"