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.
-
Example 1: the user types
Selena
Your name? ==>Selena Hi, Selena, welcome to cs111!
- Example 2: the user types
Calvin and Hobbes
Your name? ==>Calvin and Hobbes Hi, Calvin and Hobbes, welcome to cs111!
Task A2
Another useful built-in python function is
len
. len
tells you how long a given string is.
For example,
-
len('hello')
evaluates to 5
and
-
len('chocolate cake')
evaluates to 14 (spaces and punctuation count).
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:
- Example 1: the user types
Selena
Your thoughts? ==>Selena 6
- Example 2: the user types
Sometimes you feel like a nut
Your thoughts? ==>Sometimes you feel like a nut 29
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.

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
- Lab 1 Home
- Part 1: Thonny intro
- Part 2: How to work on projects
- Part 3: Your first program
- Part 4: Multimedia exercises
- Part 5: Save your work