Question
In Python, how can I create a substring from the third character to the end of a string?
For example, I want something like myString[2:end]. If leaving out the second part means “to the end of the string,” does leaving out the first part mean “start from the beginning”?
Example idea:
my_string = "hello"
# desired result: "llo"
Short Answer
By the end of this page, you will understand how Python string slicing works, how to extract part of a string using [start:end], and what happens when you leave out either the start or end index. You will also see practical examples, common mistakes, and patterns used in real Python code.
Concept
In Python, a substring is usually created with slicing.
The basic syntax is:
text[start:end]
This means:
- Start at index
start - Stop before index
end - Return everything in between
Strings in Python are zero-indexed, which means:
- First character is at index
0 - Second character is at index
1 - Third character is at index
2
So if you want the substring from the third character to the end, you write:
text[2:]
If you omit the end, Python slices to the end of the string.
If you omit the start, Python starts from the beginning:
text[:4]
This returns characters from index 0 up to, but not including, index 4.
Why this matters:
- Slicing is one of the most common ways to process text in Python.
Mental Model
Think of a string like a row of numbered boxes:
"hello"
h e l l o
0 1 2 3 4
A slice is like saying:
- “Start opening boxes at this number”
- “Stop right before this other number”
So:
"hello"[2:]
means:
- Start at box
2→l - Keep going until the end
Result:
"llo"
And:
"hello"[:2]
means:
- Start from the beginning
- Stop before box
2
Result:
"he"
A useful rule to remember:
Syntax and Examples
Basic syntax
text[start:end]
Slice from the third character to the end
text = "hello"
result = text[2:]
print(result)
Output:
llo
Explanation:
his index0eis index1lis index2text[2:]starts at index2and goes to the end
Slice from the beginning to a position
text = "hello"
result = text[:2]
print(result)
Output:
he
Explanation:
Step by Step Execution
Consider this example:
word = "python"
part = word[2:5]
print(part)
Let’s trace it:
Step 1: Store the string
word = "python"
Indexes are:
p y t h o n
0 1 2 3 4 5
Step 2: Apply the slice
part = word[2:5]
This means:
- Start at index
2→t - Include index
3→h - Include index
4→o - Stop before index
5
So becomes:
Real World Use Cases
String slicing appears in many everyday Python tasks.
1. Remove a prefix
url = "https://example.com"
domain = url[8:]
print(domain)
Possible output:
example.com
2. Get a file extension
filename = "report.pdf"
extension = filename[-3:]
print(extension)
Output:
pdf
3. Extract a date part
date = "2026-05-04"
year = date[:4]
month = date[5:7]
day = date[8:]
print(year, month, day)
4. Mask sensitive data
card = "1234567812345678"
last_four = card[-4:]
masked = "**** **** **** " + last_four
print(masked)
5. Parse fixed-format text
Real Codebase Usage
In real projects, developers often use slicing as part of larger patterns.
Validation and cleanup
def normalize_phone(number):
digits = number[-10:]
return digits
This keeps only the last part of a string when inputs may contain country prefixes.
Guard clauses before slicing
def get_last_four(text):
if len(text) < 4:
return text
return text[-4:]
This avoids assumptions about input length.
Parsing structured values
def parse_log_code(code):
prefix = code[:3]
number = code[3:]
return prefix, number
Combining slicing with conditions
def remove_hash_tag(tag):
if tag.startswith():
tag[:]
tag
Common Mistakes
1. Using end like a built-in keyword
Python does not use end inside slices like this:
text = "hello"
result = text[2:end] # broken
This causes an error unless end is a defined variable.
Correct version:
result = text[2:]
2. Forgetting that the end index is excluded
Broken expectation:
text = "hello"
print(text[1:4])
# Some beginners expect "ello"
Actual result:
ell
Why: index 4 is not included.
3. Confusing character position with index
If you want the third character, the index is 2, not 3.
Comparisons
| Concept | Syntax | Includes start? | Includes end? | Typical use |
|---|---|---|---|---|
| Single character access | text[2] | Yes | Not applicable | Get one character |
| Slice with start and end | text[2:5] | Yes | No | Get a substring range |
| Slice to the end | text[2:] | Yes | No end given | Get everything after a point |
| Slice from the beginning | text[:5] | From start | No | Get a prefix |
Cheat Sheet
Quick syntax
text[start:end]
Common patterns
text[2:] # from index 2 to the end
text[:3] # from start up to index 3
text[1:4] # index 1 through 3
text[:] # whole string
text[-4:] # last 4 characters
Rules
- Python uses zero-based indexes
- Slice start is included
- Slice end is excluded
- Omit start → begin at index
0 - Omit end → continue to the end
- Slicing out of range is usually safe
Example
word = "banana"
print(word[2:]) # nana
print(word[:3]) # ban
print(word[1:5]) # anan
Remember
- Third character = index
2
FAQ
How do I get a substring from the third character in Python?
Use:
text[2:]
The third character has index 2 because Python starts counting at 0.
Does omitting the end index go to the end of the string?
Yes. In text[2:], Python starts at index 2 and continues to the end.
Does omitting the start index begin from the start?
Yes. In text[:4], Python starts from the beginning and stops before index 4.
Is the end index included in a Python slice?
No. The end index is excluded.
"hello"[1:4] # "ell"
Can I use slicing on lists too?
Yes. The same syntax works on lists, tuples, and other sequence types.
What happens if the slice is out of range?
Python usually returns as much as possible instead of raising an error.
"hi"[:]
Mini Project
Description
Build a small Python utility that extracts useful pieces of text from a product code. This demonstrates how slicing can pull out prefixes, numeric sections, and suffixes from fixed-format strings, which is common in scripts and backend systems.
Goal
Write a program that slices a product code into meaningful parts and prints them clearly.
Requirements
[ "Store a product code string in a variable.", "Extract the first 3 characters as a category.", "Extract the middle numeric portion as an ID.", "Extract the last 2 characters as a region code.", "Print each extracted part with a label." ]
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.