Does anyone have a nice concise way of fully justifying a string for a given line width like this:
The cat sat on the mat, the cat
sat on the mat, the cat sat on
mat, the cat sat on the mat the
mat, the cat sat on the mat
the cate sat on the mat...
So that edges are straight on both sides. The function takes in a string and a line width and returns a string in the above format. There are Python functions for left, right and centre but not justified. I have a method but its a bit too long.
Thanks!
JT
asked Apr 3, 2013 at 14:59
jtaylor94
1372 gold badges2 silver badges7 bronze badges
-
3It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive.Martijn Pieters– Martijn Pieters2013年04月03日 15:02:23 +00:00Commented Apr 3, 2013 at 15:02
-
1@MartijnPieters: Or point to some code that already exists.RichieHindle– RichieHindle2013年04月03日 15:03:14 +00:00Commented Apr 3, 2013 at 15:03
1 Answer 1
Here's a recipe in the ActiveState Python Cookbook for exactly that:
"Align string with spaces between words to fit specified width"
glglgl
91.5k13 gold badges157 silver badges230 bronze badges
answered Apr 3, 2013 at 15:01
RichieHindle
284k49 gold badges367 silver badges408 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
cvanelteren
Link is dead. Please provide full code answers
lang-py