title: How to truncate a string using str.format in Python?
body: <p>How to truncate a string using <a href="https://docs.python.org/3/library/stdtypes.html#str.format" rel="noreferrer"><code>str.format</code></a> in Python? Is it even possible?</p>
  
  <p>There is a <code>width</code> parameter mentioned in the <a href="https://docs.python.org/3/library/string.html#format-specification-mini-language" rel="noreferrer">Format Specification Mini-Language</a>:</p>
  
  <pre><code>format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
  ...
  width       ::=  integer
  ...
  </code></pre>
  
  <p>But specifying it apparently only works for padding, not truncating:</p>
  
  <pre><code>&gt;&gt;&gt; '{:5}'.format('aaa')
  'aaa  '
  &gt;&gt;&gt; '{:5}'.format('aaabbbccc')
  'aaabbbccc'
  </code></pre>
  
  <p>So it's more a minimal width than width really.</p>
  
  <p>I know I can slice strings, but the data I process here is completely dynamic, including the format string and the args that go in. I cannot just go and explicitly slice one.</p>