Lesson 9 of 60
โฎPrev Nextโฏ

PY-101 ยท Strings and input

Strings and f-strings

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

9of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Strings and f-strings, running
    1. name = " Python Academy "
    2. clean = name.strip()
    3. print(clean)
    4. print(len(clean))
    5. print(clean.upper())
    6. print(clean[0], clean[-1])
    7. print(clean[0:6])
    8. print(clean.replace("Academy", "Lab"))
    9. price = 1499.5
    10. print(f"{clean} costs โ‚น{price:,.2f}")
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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?
    2. The Python ideas[i] s[start:end] len(s)A string is text, stored as a sequence of characters you can index and slice.
    3. 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}"

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. If s = "Python", what is s[1]?
    2. 2. If s = "Python", what is s[-1]?
    3. 3. What is "data"[0:2]?
    4. 4. What does f"{3.14159:.2f}" give?
    5. 5. s = "hi"; s.upper(); print(s) shows?

    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 write name = 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