← All coding practice

Coding Practice · Beginner · October 27, 2026 · 5 min read

Python Coding Exercise #61: A Framed Word

Part of the series Python Practice Exercise — part 61

Before you start: Know input() and len() · Know string multiplication · Know string methods

The question

Please write a program which asks the user for a string and then prints out a frame of * characters with the word in the centre. The width of the frame should be 30 characters. You may assume the input string will always fit inside the frame.

If the length of the input string is an odd number, you may print out the word in either of the two possible centre locations.

Word: testing
******************************
*          testing           *
******************************
Word: python
******************************
*           python           *
******************************

Before coding: the parts of the frame

The frame has three lines, and each one is exactly 30 characters wide:

LineWhat it contains
Top30 asterisks
MiddleAn asterisk, then 28 characters holding the word, then an asterisk
Bottom30 asterisks

The top and bottom lines can be made with string multiplication, as in Exercise #54: "*" * 30.

The middle line is the interesting part. The 2 asterisks at the edges use up 2 of the 30 characters, so the word has 28 characters of space in the middle. The word has to sit in the centre of that space, with the rest filled with spaces on both sides. For testing (7 characters), there are 28 − 7 = 21 spaces to share between the left and the right.

My Solution

word = input("Word: ")
print("*" * 30)
print("*" + word.center(28) + "*")
print("*" * 30)

How My Solution Works

1. The top and bottom lines

print("*" * 30) repeats the asterisk 30 times, and it is used for both the first and the last line.

2. center(28) does the centring

The string method center() puts a string in the middle of a given width, and fills the space on both sides. word.center(28) returns a new string of 28 characters, with the word in the middle and spaces on the left and right. The original word doesn’t change, because strings are immutable.

By default, center() fills with spaces, and that is what we want here. It can also take a fill character as a second argument, such as center(28, "-"). You can read more about it in the center() section of String Methods in Python.

3. Concatenation

"*" + word.center(28) + "*" puts an asterisk on each side of the centred text. The result is exactly 30 characters, and print() shows it.

Following the two samples

WordLengthSpaces to shareLeftRightLine
testing7211011* testing *
python6221111* python *

For python, the 22 spaces split evenly. For testing, 21 is an odd number, so the two sides can’t be equal, and center() puts the extra space on the right. The exercise accepts either side.

University Solution

word = input("Word: ")

print("*" * 30)
spaces_at_start = (28 - len(word)) // 2
spaces_at_end = spaces_at_start

# If the word length is odd, one is added to the spaces at the end of the word
# to get all 30 characters filled
if len(word) % 2 != 0:
    spaces_at_end += 1

print("*" + spaces_at_start * " " + word + spaces_at_end * " " + "*")
print("*" * 30)

The University Solution doesn’t use center(). It works out the number of spaces by hand:

  1. 28 - len(word) is the number of spaces to share between the two sides.
  2. // 2 is floor division, which divides and drops any remainder (see Exercise #12). It gives the number of spaces for the left side. For testing, 21 // 2 is 10.
  3. spaces_at_end = spaces_at_start starts the right side with the same number.
  4. The if fixes the odd case. When the word has an odd length, 28 minus the length is odd, and half of it leaves one space over. len(word) % 2 != 0 checks for an odd length, and spaces_at_end += 1 adds the leftover space to the right side.
  5. The last print() builds the line with string multiplication: spaces_at_start * " " makes the left spaces, and spaces_at_end * " " makes the right ones.

For testing, that gives 10 spaces on the left and 11 on the right, exactly like center().

center() or by hand?

center(28)By hand
Lines of codeOneSeveral
Odd lengthsHandled by the methodNeeds an if
What you need to knowThat the method existslen(), //, % and *

Both programs print identical output for every word length from 0 to 28, including the odd ones. Doing it by hand is a good way to see what center() does behind the scenes, and the method is shorter to write and to read.

Things to notice

The frame is always 30 characters wide. For any word of 28 characters or fewer, the middle line is also 30 characters, because the word and the spaces together fill the 28 spaces.

An empty word gives a line of spaces. If the user presses Enter without typing anything, the middle line is an asterisk, 28 spaces and an asterisk.

A longer word doesn’t fit. The exercise says the word will always fit. If it doesn’t, center() returns the word as it is, and the middle line becomes wider than the frame. For a word of 31 characters, the middle line has 33 characters.

Why % 2 works here. The space available, 28, is even. So the spaces left over are even exactly when the word’s length is even, and odd when the length is odd. That’s why checking the length for oddness is enough.

Key takeaways

  • text.center(width) returns a new string with the text in the middle of the given width, filled with spaces (or another single character).
  • A frame can be built from string multiplication: "*" * 30 makes a line of 30 asterisks.
  • // is floor division, and it divides and drops the remainder.
  • % 2 != 0 checks whether a number is odd.
  • If the spaces can’t be split evenly, one side gets an extra space.
  • A string method returns a new string and leaves the original unchanged.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · strings · string-methods · center · string-multiplication