Exercises
Exercise 1
Python provides a built-in function called len
that returns the length of a string, so the value of len('allen')
is 5.
Write a function named right_justify
" that takes a string named word
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.
>>> right_justify('allen')
allen
Exercise 2
This can be done using only the statements and other features we have learned so far.
Write a function
two_by_two_grid
that draws a grid like the following:
+ - + - +
| | |
+ - + - +
| | |
+ - + - +
>>> print('line 1 \nline 2')
line 1
line 2
>>> print('line 1' + '\n' + 'line 2')
line 1
line 2
>>>
A print
statement all by itself ends the current line and goes to the next line.
Write another function
four_by_four_grid
to draw a similar grid with four rows and four columns.
Write a more generic function
x_by_y_grid(rows, cols)
that draws a similar grid withrows
rows andcols
columns.
Last updated