I wandered into #python on irc, not finding a super-obvious way (like a method on the string) to tell if my string had any spaces. So, I wondered what the best way would be. We came up with three candidates and Devin Jeanpierre aka ssbr ran timeit for us on them. Here are his results:
>>> import timeit
>>> reg = timeit.Timer('re.search(r"\s", yourstring)', 'import re, string; yourstring = string.letters + "\\n"')
>>> nonreg1 = timeit.Timer('(" " in mystr or "\\t" in mystr or "\\n" in mystr)', 'import re, string; mystr = string.letters + "\\n"')
>>> nonreg2 = timeit.Timer('any(s.isspace() for s in yourstring)', 'import re, string; yourstring = string.letters + "\\n"')
>>>
>>> min(reg.repeat()), min(nonreg1.repeat()), min(nonreg2.repeat())
(5.0831290010322547, 0.70382800048609084, 23.529399406907856)
So, for this case a simple if (" " in mystr or "\\t" in mystr or "\\n" in mystr) won. if any(s.isspace() for s in yourstring) lost badly and would probably lose worse in larger strings. if re.search(r'\s', yourstring) proved a decent choice that would compare more favorably as the use case got more complex.