String Slicing

Python · Strings

Theory

Slicing extracts part of a string with s[start:stop:step]. Indexes start at 0, start is included and stop is excluded, so for s = 'TechTown', s[0:4] is 'Tech' (positions 0, 1, 2, 3). Leave start out to begin at the start, leave stop out to go to the end, so s[4:] is 'Town'. Negative indexes count from the end: s[-1] is 'n' and s[-4:] takes the last four characters.

The step picks every n-th character: s[::2] gives 'TcTw', and a negative step walks backwards, so s[::-1] reverses the string. Slicing is forgiving: out-of-range bounds are clipped instead of raising errors, so s[100:] is just '' (while plain indexing s[100] raises IndexError). A slice always creates a new string; the original is unchanged. The exact same syntax works on lists and tuples.

Why we use it

Data often hides useful pieces at fixed positions: the year in '2024-03-15', a prefix code, the last 4 digits of a number. Slicing pulls them out in one short, fast expression without loops.

When to use it

Use slicing for fixed-position extraction, prefixes and suffixes, and quick reversal. When the position depends on content (like 'everything after @'), combine it with .find() or use .split() instead.

How it works

  1. Evaluate s[0:4]
  2. Is any bound negative? Yes: Convert it: add len(s) to it. No: Use it as given.
  3. Clip start and stop into the range 0..len(s)
  4. Take characters from start up to (not including) stop, moving by step
  5. Return the new string 'Tech'

Syntax

string[start:stop:step]

Example

name = 'TechTown'
print(name[0:4])
print(name[::-1])

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic