My task is to verify whether string is valid date and time or not.
\"2013-01-01W23:01:01\"
The date and the time are separated with
Use str.partition() to get the first part, fast:
value.partition('W')[0]
You could use str.split() as well, for Python versions < 2.5. Limit the split to just the first W
:
value.split('W', 1)[0]
Either technique will always yield a result, even if there is no 'W'
character in value
.
Demo:
>>> value = "2013-01-01W23:01:01"
>>> value.partition('W')[0]
'2013-01-01'
>>> value.split('W', 1)[0]
'2013-01-01'