Module 03 β Strings & User Input
Slicing, formatting, and reading what the user types.
Prerequisite: Module 02 β Print & Basic Output
After this lesson, you will be able to
- Read a value typed by the user with input(), and convert it with int()/float()
- Build strings with concatenation and f-strings
- Index and slice strings to extract characters and substrings
- Use common string methods: strip(), find(), replace(), center()
Concept & Syntax
Strings are how Python represents text, and almost every real program needs to accept input from a person.
input() pauses your program, waits for the user to type something and press Enter, and returns
exactly what they typed β as a string, always. That last point trips up nearly every beginner at least
once: if you ask for someone's age and plan to do math with it, you must convert it first.
Strings in Python are also sequences β ordered collections of characters β which means every character has a numbered position (its index), starting at 0. That's what makes slicing possible.
name = input("Prompt: ")
age = int(input("Age: "))
greeting = f"Hello, {name}!"
first_three = name[0:3]Examples
Reading input Very Easy
name = input("What is your name? ")
print("Hello, " + name + "!")input() always returns a string, even if the user types digits.
Converting input to numbers Easy
age = int(input("Age: "))
next_year = age + 1
print("Next year you will be", next_year)int() converts a numeric string like "25" into the integer 25. Without it, age + 1 would crash.
f-strings Easy
name = "Ada"
score = 97.5
print(f"{name} scored {score}%")
print(f"Rounded: {score:.0f}%")An f-string (prefix f) lets you embed variables directly inside {curly braces}, including format specifiers like :.0f for rounding.
Indexing and slicing Intermediate
name = "Python"
print(name[0])
print(name[-1])
print(name[1:4])
print(name[::2])Index 0 is the first character, -1 is the last. [1:4] takes indices 1 up to (not including) 4. [::2] takes every 2nd character using a step.
Useful string methods Real World
raw = " ada@example.com "
clean = raw.strip()
print(clean)
print(clean.find("@"))
print(clean.replace("example", "python"))strip() removes leading/trailing whitespace (common when cleaning user input), find() returns the index of a substring (or -1), and replace() swaps text.
Visual Explanation
π‘ Why does this work?
Read the concept explanation above again slowly, line by line β every keyword there maps directly onto a line of code in the examples. If it still doesn't click, re-run the traced example (if this lesson has one) one step at a time.
π What happens internally? (advanced)
Under the hood, Python compiles your source into bytecode and runs it on a virtual machine (the CPython interpreter). Variables are names in a namespace dictionary pointing at objects in memory β which is exactly what the memory panel in the tracer above is showing you.
Common Mistakes
input() always returns text. Adding a string and an integer directly raises a TypeError.
age = input("Age: ")
print(age + 5)age = int(input("Age: "))
print(age + 5)Strings are <strong>immutable</strong> β you cannot change one character in place. Methods like replace() and upper() always return a brand-new string.
name = "python"
name[0] = "P"name = "python"
name = "P" + name[1:]- Prefer f-strings over + concatenation β they're more readable, especially with several variables.
- Always convert input() before doing math: int(input(...)) or float(input(...)).
- Use .strip() on user-typed input to remove accidental leading/trailing spaces.
- Remember slicing never raises an error for out-of-range indices β it just returns as much as it can.
Exercise & Challenge
Exercise 1 β Predict the output
word = "handbook"
print(word[2:6])What does this print?
π Challenge
Ask the user for their first and last name (two separate input() calls) and print a formatted greeting using an f-string, with the name in Title Case.
- Use two input() calls
- Combine both names into one f-string
- Use .title() to fix inconsistent capitalization
π‘ Need a hint?
"ada LOVELACE".title() becomes "Ada Lovelace".
π Show a sample solution
first = input("First name: ")
last = input("Last name: ")
full = f"{first} {last}".title()
print(f"Welcome, {full}!")Quiz
Answer every question, then submit to see your score and explanations.
1. What type does input() always return?
2. What is "Python"[-1]?
3. What does "Python"[1:4] return?
4. Why does name[0] = "P" raise an error?
5. Which method removes leading and trailing whitespace from a string?
Summary
- input() reads text from the user and always returns a string.
- Convert input with int()/float() before doing arithmetic on it.
- Strings support indexing ([i]) and slicing ([start:stop:step]); indices start at 0.
- Strings are immutable β methods like replace() return new strings rather than modifying the original.