PY-101 ยท Strings and input
Strings and f-strings
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A string is text, stored as a sequence of characters you can index and slice. String methods such as upper(), strip() and replace() return new strings without changing the original. An f-string, written f"Hi {name}", puts values straight into text and can format numbers.
- Index and slice strings
- Use common string methods
- Format text and numbers with f-strings
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
name = " Python Academy "clean = name.strip()print(clean)print(len(clean))print(clean.upper())print(clean[0], clean[-1])print(clean[0:6])print(clean.replace("Academy", "Lab"))price = 1499.5print(f"{clean} costs โน{price:,.2f}")
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Python Academy
14
PYTHON ACADEMY
P y
Python
Python Lab
Python Academy costs โน1,499.50 ๐ฏ first and last are lower case. Use .title() and an f-string to print: Lovelace, Ada
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
print(f"{last.title()}, {first.title()}")
- The problemEvery receipt says something like "Thank you, Priya! You paid โน1,499.50". The name and amount change, the sentence does not. How does the app fill in the blanks?
- The Python idea
s[i] s[start:end] len(s)A string is text, stored as a sequence of characters you can index and slice. - In real lifeEvery invoice, receipt and email template fills names and amounts into fixed text. That is an f-string, or the same idea in another language.
Start with a question
Every receipt says something like "Thank you, Priya! You paid โน1,499.50". The name and amount change, the sentence does not. How does the app fill in the blanks?
Most data people see is text: names, messages, reports, prompts sent to AI models. Cleaning and formatting strings is daily work.
Explain it like I'm 10
A string is a row of characters, each in a numbered seat that starts at 0. An f-string is a sentence with blanks in curly brackets, and Python fills them in for you.
The technical explanation
Positions start at 0. s[0] is the first character and s[-1] the last. s[0:6] takes positions 0 to 5; the end position is not included.
Strings cannot be changed in place. s.upper() gives you a new string, so store it: s = s.upper().
strip() removes spaces from both ends, which is the first thing to do with text a user typed.
In an f-string, anything in {} is worked out and inserted. {price:,.2f} adds thousands separators and two decimal places.
Syntax
s[i] s[start:end] len(s)
s.upper() s.lower() s.strip() s.replace(old, new)
f"text {value} more {number:.2f}" Real-world scenario
Every invoice, receipt and email template fills names and amounts into fixed text. That is an f-string, or the same idea in another language.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: User input and Lab 01: Calculator โQuestions people ask
What is an f-string in Python?
An f-string is a string literal with f before the quote, such as f"Total: {total}". Expressions inside the braces are evaluated and inserted. They were added in Python 3.6 and are the usual way to format text today.
How do I format a number to 2 decimal places in Python?
Use an f-string with a format spec: f"{value:.2f}". Add a comma for thousands separators: f"{value:,.2f}" turns 1499.5 into 1,499.50.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is an f-string in Python?
An f-string is a string literal with f before the quote, such as f"Total: {total}". Expressions inside the braces are evaluated and inserted. They were added in Python 3.6 and are the usual way to format text today.
beginner How do I format a number to 2 decimal places in Python?
Use an f-string with a format spec: f"{value:.2f}". Add a comma for thousands separators: f"{value:,.2f}" turns 1499.5 into 1,499.50.
intermediate If s = "Python", what is s[1]?
y. Indexes start at 0, so s[1] is the second character, y.
intermediate If s = "Python", what is s[-1]?
n. Negative indexes count from the end; -1 is the last character.
Quick revision
Remember
- Index and slice strings
- Use common string methods
- Format text and numbers with f-strings
- String methods never change the original.
name.upper()gives you a new string, so writename = name.upper()if you want to keep it.
Syntax
s[i] s[start:end] len(s)
s.upper() s.lower() s.strip() s.replace(old, new)
f"text {value} more {number:.2f}" Official documentation
- Text sequence type: str (docs.python.org)
- Formatted string literals (docs.python.org)