Lab 1: Text in boxes

Overview

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:

input('Type your name ==>')

produces

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

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

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

and

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

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 [OPTIONAL]: Draw a box around TWO user inputs

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: Python's built-in max function can be helpful here.

>>> max(10, 2) 
10
>>> max(2, 25) 
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