Search code examples
pythonjustify

thinkpython2 right justify column 70 meaning


thinkpython2thinkpython2 link has exercise 3.1 that specifies a function right_justify should print a string s with the last letter of the string in column 70.

The exact question is: Exercise 3.1. Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.

There is also a hint provided: Hint: Use string concatenation and repetition. Also, Python provides a built-in function called len that returns the length of a string, so the value of len('monty') is 5.

Is there a standard for determining so-called column locations? How would you write the code??

Example Code:

>>>right_justify('monty')
                                                                       monty

Solution

  • To determine what "column" the last letter of a string is in is the same as its length.

    From the pdf: "Hint: Use string concatenation and repetition. Also, Python provides a built-in function called len that returns the length of a string, so the value of len('monty') is 5."