Lesson 3 of 30
In progress
Strings, f-strings and reading input
Join text together, build readable messages with f-strings, and read what a learner types without your program breaking.
Most programs talk to a person: a name on a certificate, an error message, a total on a receipt. Text handling is therefore not a side topic. It is the part of your program the user actually sees.
What you will learn
- Create strings and join them without accidental spaces.
- Format values with f-strings, including numbers and alignment.
- Read input and convert it before doing maths.
Text is a sequence of characters
A string is text in quotes. Single and double quotes both work, as long as they match. Use double quotes by default, so an apostrophe inside the text does not end the string early.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
The first print joins three strings with + and you must add the space yourself. The second passes two values to print, which adds a space between them. Both are fine; mixing them up is how you get HelloRavi.
Triple quotes let a string run across several lines:
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
f-strings put values inside text
An f-string starts with f before the opening quote. Anything inside braces is evaluated and inserted.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
The braces can contain expressions, not just names. This is why f-strings replaced most older formatting for beginners: the text and the values sit together and read like a sentence.
They also format numbers. :.2f shows two decimal places, and :, adds thousands separators.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
:.2f rounds for display only. It does not change the stored value. That distinction matters when you calculate with money: display is not storage.
Read input, then convert it
input() shows a prompt and returns whatever the person typed, always as a string.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
The space before the closing quote in the prompt is deliberate. Without it, the cursor sits against the question mark.
Numbers need converting before arithmetic:
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
If the learner types twelve, int() raises ValueError and the program stops. That is a real problem in a program other people use, and module 4 shows how to catch it. For now, notice that conversion is a separate, deliberate step.
Use strip() to remove accidental spaces at the ends, which are invisible and cause confusing comparisons:
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
if answer == "yes":
print("Continuing")
else:
print("Stopped")
Press Run to see the output.
lower() makes the comparison ignore capitalisation. strip() removes spaces the user did not mean to type. Together they make text input much more forgiving.
Indexing reads a single character with square brackets: word[0] is the first character and word[-1] is the last. Negative indexes count from the end, which is handy for trimming punctuation from a typed answer. Strings are immutable, so word[0] = "P" fails with a TypeError; you build a new string instead.
Do not compare text with is. answer is "yes" asks whether two objects are the same object in memory, not whether the text matches. It can appear to work in small tests and fail later. Use == for text comparison, always.
Ask for a first name and a last name separately, strip both, then print a single line in the form Last, First followed by the number of characters in the full name without spaces.
Show solution
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
len() counts characters, including spaces, so building full without a space gives the count of letters. If you want to allow a middle name later, the same pattern still works.
Recap
- Strings hold text;
+joins andprint(a, b)adds a space for you. - f-strings insert expressions and format numbers for display.
input()always returns text, so convert withint()orfloat()and tidy it withstrip().
Next, you will work with numbers properly: division that returns a decimal, integer division, remainders, and the rounding surprises that catch everyone.
What is the difference between an f-string and string concatenation?
Why does input() return text even when I type a number?
Why does len count differently for some Hindi or emoji text?
Progress is saved in this browser only. No account, no email.