Lab 2: Part 2: Text in boxes

Note on Labelling

The sub-tasks in this section are marked Partner A or Partner B to indicate how to work on them as a pair and when you should switch "drivers." One of the members of your team should be writing code for the parts marked Partner A and then the other member should write code for the parts marked Partner B, to ensure you both get hands-on practice with today's concepts. If you happen to be in a team of 3, just switch who is writing code for each different segment, but make sure all members of the team get a chance to write code. You may have to dictate some code to each other when one part depends on a previous part, but we've arranged the parts to minimize this. Of course, whoever is not writing code should be helping out by suggesting things to try and helping to proofread the code that's being written.


You'll first start with built-in python functions input and len, and then combine those functions to draw boxes around a user-supplied sentence.

Task A: Get comfy with input

The python built-in function input allows you to ask the user for input. Here is a list of all the built-in Python functions and here is our quick-reference page for the built-in functions you'll need in this course.

Try this:

input('Type your name')  

produces this:

Type your name      # notice that python is waiting for you to respond

Note: you can add an arrow to the prompt, if you like, and it's usually useful to add a space at the end of the prompt, so that the user doesn't naturally type a space themselves, which would count as part of their input:

input('Type your name ==> ')


Type your name ==>  # waiting for you to type something and hit return

Let's say the user types Selena. In order to refer to Selena later in your program, you'll need to save it in a variable.

Task A1

Partner A

Write a couple of lines of Python that asks the user for their name, and then prints a friendly greeting. Here are some examples.

Task A2

Partner B

Another useful built-in python function is len. len tells you how long a given string is. For example,

Write a couple of lines of python that asks the user for their thoughts, and then prints the length of the user's thoughts. Below are some examples of how your program should work:

Now that you are familiar with both input and len, we can do something a bit more complex.

Task B: Draw a box around the user input

Partner A

In this task, you will write a short Python program that asks the user to provide a string, and then prints a box of asterisks (*) around the string.

Below are three different examples of what your program should do:

Sample 1:

Enter a string ==> Hi
*     Hi     *

Sample 2:

Enter a string ==> Lovely day today
*     Lovely day today     *

Sample 3:

Enter a string ==> Sometimes you feel like a nut
*     Sometimes you feel like a nut     *

Let's take another a close look at Sample 3 above.

There should be 5 spaces of padding on either side of the string in the box (see enlarged image below). Note that your program should work correctly regardless of the length of the provided string.

An image illustrating the number of spaces and counting each space to show that there are exactly 5 on each side of the middle text. If you can configure your screen reader or other display to show per-character information, this information should be availble from the text above as well.

Task C: Draw a box around TWO user inputs

Partner B

In this task, the user is asked TWICE to provide strings. Your program will produce a box around both lines of text, with 5 spaces of padding around the longer string in the box. See examples below.

Hint: You will need Python's built-in max function.

>>> max(10, 2) 
>>> max(2, 25) 

Example 1:

Enter your first string ==> happy

Enter your second string ==> birthday to you!
*     happy                *
*     birthday to you!     *

Example 2:

Enter your first string ==> Row row row your boat

Enter your second string ==> gently
*     Row row row your boat     *
*     gently                    *

Example 3:

Enter your first string ==> Sometimes you feel like a nut

Enter your second string ==> Sometimes you do not
*     Sometimes you feel like a nut     *
*     Sometimes you do not              *

Table of Contents