in Python, is there a way to break a wrapping, centered text prhase so that there is not a single word left hanging under a long string?
<p>in Python, is there a way to break a wrapping, centered text prhase so that there is not a single word left hanging under a long string?</p>

<p>Example, assuming this is centered:</p>

<p>**West Dakota Department of Fish and </p>

<pre><code>        Game**
</code></pre>

<p>The word "Game" is short, and the string above is long.  Does not look good as a header for a website. Would like a method to instead display the following:</p>

<p>**West Dakota Department</p>

<p>of Fish and Game**</p>

<p>Maybe using javascript?</p>

<p>a couple of things, you could get fancy and parse the length of the longest sub-string as well rather than specifying the format width (eg. ^20) in the centering portion of the expression</p>

<pre><code>&gt;&gt;&gt; import textwrap
&gt;&gt;&gt; a = "West Dakota Department of Fish and Game"
&gt;&gt;&gt; wdth = int(len(a)/2)
&gt;&gt;&gt; args = textwrap.wrap(a, wdth)
&gt;&gt;&gt; print(("{!s:^20}\n"*len(args)).format(*args))
    West Dakota     
 Department of Fish 
      and Game  
</code></pre>

python