Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Strings · Lesson 5 of 25

    Strings and f-strings

    Put variables inside text, plus handy string tools.

    6 minutes3 games

    An f-string: put f before the quote and write variables inside { } in the text. The clearest way to build text.

    Python
    name = "Karim"
    score = 92
    print(f"{name} scored {score} points")
    print(f"Next goal: {score + 5}")
    
    word = "seacode"
    print(word.upper(), len(word), word[0], word[-1])

    Important

    Strings are immutable: formatting and case conversion return new strings without changing the original.

    Interview question

    Why use an f-string instead of concatenation?

    ToolUse

    len(word)

    Character count

    word[0]

    First character

    f"{price:.2f}"

    Format a number to two decimal places

    Python
    price = 12.5
    qty = 3
    print(f"Total: {price * qty:.2f} EGP")

    word[-1] is the last character and word[0] the first. Python counts from 0.

    Complete the code

    Complete the f-string so it prints Hi Laila.

    user = "Laila"
    print("Hi {}")

    What's the output?

    print(len("Cairo"))

    What will this code print?

    Exercise

    Print Total: 150 EGP with an f-string from the two variables.

    Expected output: Total: 150 EGP

    Python
    price = 50
    qty = 3
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousType conversion and inputNextString indexing and slicing
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.