University of Glasgow · FACULTY OF COMPUTER SCIENCE

COMPSCI4039 Chap.7 Input and String Formatting

- one subject, every graph, every model, every mark
7 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 7 of 10 · COMPSCI4039

Input and String Formatting

Input and String Formatting gathers the tools for getting text into a program and laying results out neatly. Strings have their own operators: plus joins two strings without adding a space, and star repeats a string a given number of times. Special characters start with a backslash, such as the newline and the tab, and print ends each call with a newline unless told otherwise with end.

Keyboard input uses a single call that shows a prompt, waits for Enter and returns the typed text as a string, so numbers must be cast with int or float before arithmetic. When one line carries several values, split breaks it at whitespace into a list of strings. For output, concatenation produces ragged columns because values differ in length, so the course introduces f-strings.

Inside the braces, a format specification sets a minimum width, alignment with the greater-than or less-than sign, zero padding for integers and a number of decimal places for floats, with the decimal point counted as part of the width. A value longer than its field is shown in full, never truncated.

In this chapter

What this chapter covers

  • 01

    String concatenation and repetition

  • 02

    Special characters and print's end argument

  • 03

    Reading keyboard input as strings

  • 04

    Casting and splitting typed input

  • 05

    Format specifications inside f-strings

  • 06

    Width, alignment and zero padding

  • 07

    Decimal places and aligned columns

Worked example · free

Lay out one row of a fixed-width table

Q [3 marks]. Using ~ for spaces, what does f"{'Oban':<6}{12:>4}{3.14159:7.2f}" produce?
Each square is one character of the result.Each square is one character of the result. The first field pads Eve with five spaces to fill eight positions, and the second right-aligns 103 in a field of five, so the line is 13 characters wide.Eve103{name:>8}{age:5}5 pad spaces2 pad spaces
Each square is one character of the result. The first field pads Eve with five spaces to fill eight positions, and the second right-aligns 103 in a field of five, so the line is 13 characters wide.
  • 1'Oban' left-aligned in 6 characters becomes Oban~~. We use this weighting for practice only; it is not a university marking scheme.
  • 112 right-aligned in 4 characters becomes ~~12.
  • 13.14159 to two decimal places is 3.14, four characters, padded to 7 as ~~~3.14.
The result is Oban~~~~12~~~3.14, seventeen characters in all.
Sia tip — Write each field separately, count its characters, and only then join the fields together.
Glossary

Key terms

Newline character
The special character written as backslash n that moves output to the next line.
Format specification
The part after a colon inside f-string braces that controls width, alignment and precision.
Concatenation
Joining strings end to end with the plus operator.
FAQ

Input and String Formatting FAQ

Why does input give me a string even when I type a number?

input always returns exactly what was typed as text. To calculate with it, wrap the call in int or float, as in int(input()). Without the cast, multiplying the value repeats the text instead of doing arithmetic.

What does the number after the colon mean in an f-string?

It sets the minimum width of the field in characters. Shorter values are padded with spaces, on the left for numbers by default and on the right for strings, while longer values are printed in full without being cut.

How do I show exactly two decimal places?

Use the f type with a precision, as in {value:.2f}, which rounds the displayed value to two places. Adding a width in front, such as {value:8.2f}, also pads the result to eight characters, counting the decimal point.

How do I keep printed output on the same line?

print normally ends with a newline, so each call starts a fresh line. Passing end="" replaces that newline with nothing, and any other string can be supplied instead, such as a space. Nested loops that draw shapes rely on this, printing a row of symbols on one line before a plain print finishes the row.

Study strategy

Exam move

Formatting questions are won by literal counting, so practise that counting until it is automatic. Make a grid with one square per character and fill it in for each specification you meet, marking spaces with a tilde as the examples sheet does.

Work through a mixed set: strings with greater-than and less-than alignment, integers with plain width and with zero padding, and floats with different precisions and widths, including cases where the value is already wider than its field. Check each answer in the interpreter by printing the result between two vertical bars so that padding becomes visible.

For input, write small programs that read a line, split it into parts, cast the numeric parts and print a sentence, then test them with too few and too many words to see what fails. Finish by building a small aligned table, such as a times table or a receipt, choosing every width from the longest value in its column, and confirm that the decimal points line up.

Working through Input and String Formatting in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Input and String Formatting question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 26 of your subjects at University of Glasgow, plus 1,000+ more Bibles across the AskSia library.
Sia - your COMPSCI4039 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works