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.
- It is used in parsing, cleaning data, extracting prefixes or suffixes, and formatting strings.
- The same slicing idea also works with lists, tuples, and other sequence types.
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:
- Left side included
- Right side excluded
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:
- No start index means “start at the beginning”
- Stop before index
2
Slice between two positions
text = "hello"
result = text[1:4]
print(result)
Output:
ell
Explanation:
- Start at index
1→e - Stop before index
4 - Returned characters:
e,l,l
Full copy of a string
text = "hello"
result = text[:]
print(result)
Output:
hello
This means “from the beginning to the end.”
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 part becomes:
"tho"
Step 3: Print the result
print(part)
Output:
tho
Another quick trace
word = "python"
print(word[2:])
This starts at index 2 and keeps going:
thon
Output:
thon
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
record = "USR001Alice "
user_id = record[:6]
name = record[6:].strip()
print(user_id, name)
This is common in scripts that read structured text files.
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("#"):
return tag[1:]
return tag
Using slicing with other sequence operations
Slicing works not only with strings, but also with:
- lists
- tuples
- bytes
That makes it a common, reusable Python skill across a codebase.
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.
text = "hello"
print(text[2:]) # correct
4. Assuming slicing causes an error when indexes are too large
text = "hi"
print(text[:10])
This is valid. Python safely returns:
hi
5. Using parentheses instead of brackets
Broken code:
text = "hello"
print(text(1:3))
Correct code:
print(text[1:3])
6. Forgetting strings are immutable
You can slice a string, but you cannot change one character directly:
text = "hello"
text[0] = "H" # broken
Instead, create a new string:
text = "H" + text[1:]
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 |
| Full slice | text[:] | From start | To end | Copy the whole string |
Indexing vs slicing
text = "hello"
print(text[1]) # one character
print(text[1:4]) # substring
Output:
e
ell
Python slicing vs some other languages
In some languages, substring functions may use:
- a start index and length
- inclusive end positions
- special keywords like
end
Python slicing is different:
- it uses brackets:
[] - it uses a colon:
start:end - the end is excluded
- omitted start means beginning
- omitted end means end of sequence
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 text[2:end]is not standard Python unlessendis a variable- Use
text[2:]for “from index 2 to the end”
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"[:10] # "hi"
Can I get the last few characters with slicing?
Yes. Use negative indexes.
text[-3:]
This returns the last three characters.
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.
Add Rows to a Pandas DataFrame in Python
Learn how to add rows to a Pandas DataFrame, why repeated row appends are slow, and when to use loc, concat, or record lists.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.