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 6 of 25

    String indexing and slicing

    Select characters and slices, including their boundaries.

    8 minutes3 games

    Indexing starts at zero; negative indices count from the end. A slice text[start:stop:step] includes start and stops before stop. Omit a boundary to extend to that end of the string.

    word = "Python" =

    P[0]
    y[1]
    t[2]
    h[3]
    o[4]
    n[5]
    Python
    word = "Python"
    print(word[0], word[-1])
    print(word[1:4])
    print(word[:2], word[2:])
    print(word[::2])
    print(word[::-1])

    Important

    word[99] raises IndexError, but word[:99] stops at the end. A slice step cannot be zero.

    Interview question

    Can you assign word[0] = "J"?

    What's the output?

    word = "Python"
    print(word[1:4])

    What will this code print?

    Complete the code

    Complete the reversed string.

    print("code"[::])

    Exercise

    Extract and print 2026 from the date.

    Expected output: 2026

    Python
    date = "2026-10-25"
    

    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.

    PreviousStrings and f-stringsNextCleaning and splitting strings
    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.