2

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

2
  • 3
    It 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. Commented Apr 3, 2013 at 15:02
  • 1
    @MartijnPieters: Or point to some code that already exists. Commented Apr 3, 2013 at 15:03

1 Answer 1

3

Here's a recipe in the ActiveState Python Cookbook for exactly that:

"Align string with spaces between words to fit specified width"

Sign up to request clarification or add additional context in comments.

1 Comment

Link is dead. Please provide full code answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.