COMPSCI4039 Chap.7 Input and String Formatting
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.
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
- 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.
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.
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.
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.