Strings, Input, and Output

core-python
beginner
Work with text, formatted messages, and simple user interaction.
  • Level: Beginner
  • Estimated time: 25–40 minutes
  • You will learn: Work with text, formatted messages, and simple user interaction.
  • Practice in: Google Colab, Jupyter, or VS Code

Questions

  • What problem does Strings, Input, and Output help us solve in a small Python program?
  • What should we predict before running the example?
  • What value, output, or error should we inspect after changing one line?

Objectives

  • Run a complete example for strings, input, and output in Colab.
  • Explain the example line by line using plain language.
  • Change one part of the code and predict the result before running it.
  • Recognize one common mistake and use the error message as evidence.

Hands-on episode: Strings, Input, and Output

A string is a text value. print displays values, f-strings combine stored values with text, and input always returns text even when the person typed digits.

We will learn this by running code, not by memorizing a definition first. Open the Colab notebook from the button above, find this section, and run each cell in order. Keep a small note beside the notebook with three columns: prediction, actual result, and what changed.

Example 1.1

Predict the exact message, including spaces and punctuation.

name = "Maya"
score = 8
message = f"Hello, {name}. Your score is {score}."
print(message)

Run the cell once without editing it. If the result is different from your prediction, leave the prediction visible and write one sentence about the difference. That sentence is more useful than a perfect first guess.

Explain Example 1.1

  • The first line stores the text Maya under the name name.
  • The second line stores the integer 8 under the name score.
  • The f-string starts with f, so Python replaces {name} and {score} with their current values.
  • print(message) displays the final string, not the word message.

Now explain the example out loud or in a Markdown cell. Use short sentences: “this line creates…”, “this name stores…”, “this output appears because…”. If you cannot explain a line yet, run only the lines above it and inspect the values that exist at that moment.

Challenge 1.1

NoteChallenge

Remove the f before the string and run again. The braces will print literally. This small experiment shows that the f is not decoration; it changes how Python interprets the string.

Show a safe way to approach the challenge
  1. Copy Example 1.1 into a new Colab cell.
  2. Change exactly one value, name, condition, or line.
  3. Write the expected output before running the cell.
  4. Run the cell and compare the actual result with your prediction.
  5. If the result surprises you, undo the change and try a smaller one.

Suggested first move: Remove the f before the string and run again.

Debugging checkpoint 1.1

WarningDebugging checkpoint

The classic beginner bug is adding input directly to a number. input() returns a string, so age = input(...) gives you text. If you need arithmetic, convert intentionally with int(age) or float(age) and be ready to handle invalid input later.

Do not debug by rewriting the whole example. Read the error type or surprising output, inspect the closest value with print(...) or type(...), then change one thing. This is the same routine you will use in larger projects.

Apply it

In Colab, ask for a learner’s name and favorite topic. Print a two-sentence welcome message using an f-string. Then ask for a number, convert it, add one, and print the result.

Finish by adding a Markdown cell that answers: What did this example teach me that I can reuse in a project?

Key points

  • Learn the concept by running a complete, small example first.
  • Predict before execution so your thinking becomes visible.
  • Change one thing at a time so cause and effect stay clear.
  • Treat errors as clues about the exact line or value Python could not handle.

The problem this lesson solves

Most beginner programs need to talk with people. They display instructions, show results, and sometimes ask the user to type information. Python uses strings for text, print() for output, and input() for simple input.

A string is not just “words”. It can contain letters, digits, spaces, punctuation, emojis, and line breaks. This matters because a program often needs to build messages such as:

"Hello, Ada!"
"Your total is $12"
"Try again: password is too short"

In this lesson you will learn how text values work, how to combine them safely, and why user input must often be converted before doing math.

How input becomes output

A simple interactive program is a conversation: prompt, receive text, process, then print a reply.

flowchart LR
  prompt["input prompt<br/>What is your name?"] --> user["user types<br/>Ada"]
  user --> text["Python stores text<br/>'Ada'"]
  text --> build["build message<br/>f'Hello, {name}!'"]
  build --> output["print output<br/>Hello, Ada!"]

The important beginner detail is that input() always gives you text, even when the user typed digits.

1. Strings are text values

A string is a value that represents text. In Python, the most common way to create a string is to put quotation marks around the text.

name = "Ada"
language = "Python"
message = "Hello"

The quotation marks tell Python: “treat this as text”. The quotes are not part of the text displayed to the user; they are the boundary markers in your code.

Example 1

Before running this code, predict the four printed lines.

word = "Python"
print(word)
print(word[0])
print(word[-1])
print(f"I am learning {word}.")

Step-by-step explanation

  1. word = "Python"
    • Python creates a string value containing the six characters P, y, t, h, o, and n.
    • The name word now refers to that string.
  2. print(word)
    • Python looks up word and displays the whole string: Python.
  3. print(word[0])
    • Python strings are ordered. The first character is at index 0, not index 1.
    • word[0] is "P".
  4. print(word[-1])
    • Negative indexes count from the end.
    • word[-1] is "n", the last character.
  5. print(f"I am learning {word}.")
    • The f before the opening quote makes an f-string.
    • Anything inside {...} is evaluated by Python.
    • {word} is replaced by the value of word, so the output is I am learning Python.

Why indexes start at 0

Think of indexes as distance from the beginning. The first character is zero steps away from the beginning, so its index is 0.

string:  P  y  t  h  o  n
index:   0  1  2  3  4  5
negative -6 -5 -4 -3 -2 -1

Joining strings

You can join strings with +:

first = "Ada"
last = "Lovelace"
full_name = first + " " + last
print(full_name)

The middle string, " ", is a space. Without it, the result would be "AdaLovelace".

For longer messages, prefer f-strings because they are easier to read:

score = 95
print(f"Ada scored {score} points.")

Common mistakes

WarningCommon mistake

Mistake 1: forgetting the f in an f-string.

name = "Ada"
print("Hello, {name}!")

This prints Hello, {name}! because Python treats {name} as ordinary text. Fix it by adding f before the string:

print(f"Hello, {name}!")
WarningCommon mistake

Mistake 2: using an index that does not exist.

word = "Python"
print(word[10])

word has indexes 0 through 5. Index 10 is outside the string, so Python raises IndexError.

Check your understanding

2. Input and output

print() sends information out of a program. input() brings information in from the user.

name = input("What is your name? ")
print(f"Hello, {name}!")

When Python runs the first line, it pauses and waits. Whatever the user types is returned as a string and assigned to name.

Example 2

Try this in Google Colab or a local notebook. In Colab, use the play button on the left side of the code cell.

age_text = input("How old are you? ")
age = int(age_text)
next_year = age + 1
print(f"Next year you will be {next_year}.")

Step-by-step explanation

  1. input("How old are you? ")
    • The prompt text is displayed.
    • The user types something, such as 12.
    • Python returns the text value "12".
  2. age_text = input(...)
    • The name age_text stores that text.
  3. age = int(age_text)
    • int(...) tries to convert the text into an integer.
    • If age_text is "12", the result is the number 12.
  4. next_year = age + 1
    • Now the value is numeric, so arithmetic works.
  5. print(f"Next year you will be {next_year}.")
    • The result is placed inside the message and printed.

Why conversion matters

input() always returns a string. This surprises many beginners.

number = input("Type a number: ")
print(number + number)

If the user types 5, the output is 55, not 10, because Python is joining strings. Convert first when you need arithmetic:

number_text = input("Type a number: ")
number = int(number_text)
print(number + number)

Common mistakes

WarningCommon mistake

Mistake: trying to convert text that is not a whole number.

age = int("twelve")

This raises ValueError because Python does not know how to turn the word "twelve" into an integer. When using input(), assume users may type unexpected text and be ready to validate it later.

Try it

In the notebook, create one code cell and build a tiny greeting program:

name = input("Name: ")
city = input("City: ")
print(f"Hello, {name} from {city}!")

Then change one thing at a time:

  1. Add a third input for a favorite food.
  2. Print the final message on two lines.
  3. Ask for a number and convert it with int().

Check your understanding

Key points

TipKey points
  • Strings are text values surrounded by quotes in your code.
  • String indexes start at 0; negative indexes count from the end.
  • F-strings let you place Python values inside messages.
  • print() displays output.
  • input() returns text, so convert with int() or float() before arithmetic.

References

  • Python Tutorial: strings: https://docs.python.org/3/tutorial/introduction.html#strings
  • Python built-in functions: print, input, and int: https://docs.python.org/3/library/functions.html
  • Google Colab: https://colab.new
Back to top