Python Tutorial

Python Slicing Strings

Slicing pulls a section out of a string with [start:end:step]. Two ideas unlock everything: the end index is not included, and negative numbers count from the right.

Basic slice: [start:end]

Give a start and an end index. You get the characters from start up to — butnot includingend. Indexing begins at 0.

Example

b = "Hello, World!"
#    0123456789...
print(b[2:5])   # llo   (positions 2, 3, 4 — position 5 is excluded)

Why exclude the end? Because it makes the length easy to read off: b[2:5] is5 - 2 = 3 characters. And two slices like b[:5] and b[5:] fit together perfectly with no gap or overlap.

Leave out the start or the end

Omit start to begin at the very first character; omit end to run to the last one.

b = "Hello, World!"
print(b[:5])    # Hello   (start defaults to 0)
print(b[7:])    # World!  (end defaults to the length)
print(b[:])     # Hello, World!  (a full copy)

Negative indexing

Negative numbers count from the end: -1 is the last character, -2 the second-to-last, and so on. Handy when you care about the end of a string but don't know its length.

b = "Hello, World!"
print(b[-6:-1])  # World  (from 6th-from-last up to, not incl., last)
print(b[-1])     # !      (single index, not a slice)
print(b[-6:])    # World!

The step: [start:end:step]

A third number sets the stride. 2 takes every second character; a negative step walks backwards — which gives Python's famous one-line string reversal.

b = "Hello, World!"
print(b[::2])    # Hlo ol!   (every 2nd character)
print(b[::-1])   # !dlroW ,olleH   (reversed)
print(b[6:1:-1]) #  ,oll     (walk left from index 6 down to 2)

With a negative step, start should be to the right of end, or you get an empty string. "abc"[0:3:-1] is "" because you can't walk right-to-left from 0 to 3.

Out-of-range slices are safe

Slicing never raises an "index out of range" error — Python quietly clamps to what exists. (A single index likeb[100] does raise; a slice like b[2:100] does not.)

b = "Hello, World!"
print(b[7:999])  # World!   (clamped to the end, no error)
print(b[50:60])  # (empty string)

Slicing returns a new string

Every slice is a fresh copy — the original is never modified (strings are immutable anyway). This is why slicing is a safe way to inspect or reshape text without side effects.

word = "python"
first_three = word[:3]
print(first_three)  # pyt
print(word)         # python  (untouched)

Real uses of slicing

Grab a file extension

name = "report.pdf"
print(name[-3:])           # pdf
# more robust when the length varies:
print(name.rsplit(".", 1)[-1])  # pdf

Build initials

full = "Ada Lovelace"
initials = "".join(part[0] for part in full.split())
print(initials)   # AL

Check a palindrome

s = "racecar"
print(s == s[::-1])   # True

Quick reference

SliceMeaningOn "Hello, World!"
[2:5]Index 2 up to (not incl.) 5llo
[:5]Start through index 4Hello
[7:]Index 7 to the endWorld!
[-6:]Last 6 charactersWorld!
[::2]Every 2nd characterHlo ol!
[::-1]Reversed!dlroW ,olleH

Try It Yourself

Exercise: Given code = "PY-2024-0042", slice out the year 2024 and, separately, the last four digits 0042.

Show solution
code = "PY-2024-0042"
print(code[3:7])   # 2024
print(code[-4:])   # 0042

Key Takeaways

  • [start:end] includes start, excludes end.
  • Omit either side to go to the beginning or end; negative indexes count from the right.
  • The third value is a step; [::-1] reverses a string.
  • Slices clamp to range (no error) and always return a new string.

📘 Real-World Deep Dive

Slicing (<code>s[start:stop:step]</code>) works on every sequence — strings, lists, tuples, and even NumPy arrays. Knowing the off-by-one rules and the rare-but-useful <code>[::-1]</code> reverse is a daily multiplier.

Real-Life Scenario

A small parser that pulls out file extensions, identifies a class name from a fully-qualified Python name, and rotates a circular buffer of recent event names.

Real-Life Example

def ext_of(path: str) -> str:
    dot = path.rfind(".")
    return path[dot + 1:] if dot >= 0 and dot < len(path) - 1 else ""

def class_of(fqname: str) -> str:
    # "pkg.sub.Mod.Class"  -> "Class"
    return fqname.rsplit(".", 1)[-1]

recent: list[str] = []
def remember(name: str, maxlen: int = 5) -> None:
    recent.append(name)
    if len(recent) > maxlen:
        # keep only the last maxlen items in-place
        del recent[:-maxlen]

paths = ["report.csv", "image.PNG.bak", "noext", "archive.tar.gz"]
for p in paths:
    print(f"{p:<20}  ext={ext_of(p)!r}")

for q in ["pkg.sub.Mod.Class", "TopLevel", "a.b.c"]:
    print(f"{q:<25}  class={class_of(q)!r}")

for name in ["a", "b", "c", "d", "e", "f", "g"]:
    remember(name)
    print(f"recent={list(recent)}  last3={recent[-3:][-3:]}  reversed={[x for x in reversed(recent)]}")

Expected Output

report.csv            ext='csv'
image.PNG.bak         ext='bak'
noext                ext=''
archive.tar.gz        ext='gz'
pkg.sub.Mod.Class     class='Class'
TopLevel              class='TopLevel'
a.b.c                 class='c'
recent=['a']  last3=['a']  reversed=['a']
recent=['a', 'b']...
recent=['c', 'd', 'e']  last3=['c', 'd', 'e']...
recent=['d', 'e', 'f']  ...
recent=['e', 'f', 'g']  ...

Common mistakes

  • Confusing stop positions: xs[:3] is the first 3, xs[3:] is everything from the fourth onward.
  • Negative steps (xs[::-1]) reverse the slice; combined with positive bounds (xs[::-2]) takes every other char in reverse.
  • Slicing past the end (xs[:1000]) silently stops; use len(xs) defensively.

🚀 Performance & Best Practices

  • Slices create new objects — fine for strings, but deque is the right choice for sliding-window jobs.
  • Use str.find (returns -1) instead of str.index when the substring is optional.
  • rsplit("/", 1) beats building a generic split + slice when you only care about the last element.

🧪 Try It Yourself

  1. Implement rot_n(s, n) that rotates the first n chars to the end using slicing alone.
  2. Write suffix_of(path, n) that returns the last n chars without raising on short strings.
  3. Benchmark list(reversed(xs)) vs. xs[::-1] for length-100 lists.

FAQ: Python Slicing Strings

Common questions about this page.

What is Python Slicing Strings?

Python Slicing Strings is a Python Tutorial lesson that explains python string slicing in Python. Slicing pulls a section out of a string with [start:end:step]. Two ideas unlock everything: the end index is not included, and negative numbers count from... It is written for beginners who want a clear definition and working examples.

Should I run python string slicing examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn python string slicing in this Python Tutorial Python lesson (Python Slicing Strings).

How do I use python string slicing in Python?

To use python string slicing in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of python string slicing?

This Python Slicing Strings tutorial shows python string slicing syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Python Slicing Strings example for beginners

Yes. This page includes a beginner python string slicing example you can copy and run. It is designed for searches such as "python string slicing for beginners", "python string slicing example", and "how to use python string slicing".

What are common mistakes with python string slicing?

Common python string slicing mistakes include wrong syntax, mixing types, and skipping practice. Work through this Python Tutorial chapter in order, run every example, and check the output before moving on.

Why should I learn python string slicing?

Python Slicing Strings is used in real Python work. Learning python string slicing helps you write clearer programs and continue the Python Tutorial tutorial on StudyGrid.

Is Python Slicing Strings free to learn online?

Yes. You can learn python string slicing free on StudyGrid (studygrid.in). This chapter is part of the Python Tutorial path and includes examples, syntax, and next-step links.