title: How do I reliably split a string in Python, when it may not contain the pattern, or all n elements?
body: <p>In Perl I can do:</p>
  
  <pre><code>my ($x, $y) = split /:/, $str;
  </code></pre>
  
  <p>And it will work whether or not the string contains the pattern.</p>
  
  <p>In Python, however this won't work:</p>
  
  <pre><code>a, b = "foo".split(":")  # ValueError: not enough values to unpack
  </code></pre>
  
  <p>What's the canonical way to prevent errors in such cases?</p>