How to Get the First Characters of a String in Python with Slicing
To keep only the start of a string, such as the date in "2026-12-03T14:05:09+00:00", you don't need a function: a slice, text[:10], gives you the first 10 characters. This guide takes the first characters of a timestamp on Python 3.15.2.
Syntax
<string>[:<count>]
| Part | What it means |
|---|---|
<string> | The text, or a variable that holds it |
[:<count>] | A slice from the start up to, but not including, position <count>. That's the first <count> characters |
Python's tutorial says "slicing allows you to obtain a substring", that "an omitted first index defaults to zero", and that "out of range slice indexes are handled gracefully when used for slicing".
The Example
The script takes the first 10 characters of an ISO 8601 timestamp, which is its date, then the first 4, which is its year, and then the first 10 characters of a string that has only 2.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
timestamp = "2026-12-03T14:05:09+00:00"
print(timestamp[:10])
print(timestamp[:4])
print("Hi"[:10])
![The first_characters.py script in a macOS-style code window: the timestamp string 2026-12-03T14:05:09+00:00, then print of timestamp[:10], timestamp[:4] and "Hi"[:10].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-string-slice-01-code.webp)
timestamp[:10]takes the characters at positions 0 to 9.timestamp[:4]takes positions 0 to 3."Hi"[:10]asks for more characters than the string has.
Step 2: Run It
uv run python first_characters.py
2026-12-03
2026
Hi

The first slice is the date and the second the year. The third gave back the whole short string, with no error: a slice stops at the end of the string. The original timestamp didn't change, because a slice makes a new string. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using an index instead of a slice: timestamp[10] | You get one character, T, the one at position 10 | Add the colon: timestamp[:10] |
Slicing a datetime object instead of its text | TypeError: 'datetime.datetime' object is not subscriptable | Turn it into text first with .isoformat(), or use .date() |
Counting from 1: timestamp[:9] for 10 characters | You get one character too few | The number after the colon is how many characters you get |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: '...' object is not subscriptable | The value isn't a string | Check its type with type(value) and convert it to text first |
| The slice is shorter than asked for | The string itself is shorter | Expected: a slice never fails for being too long. Check len(text) if the length matters |
FAQ
Is [0:10] the same as [:10]?
Yes. Leaving out the first number means 0. timestamp[0:10] also prints 2026-12-03.
How do I get the last characters?
Use a negative start: timestamp[-6:] is the last 6 characters, +00:00.
Conclusion
timestamp[:10] returned 2026-12-03 and timestamp[:4] returned 2026. A slice from the start takes the first characters you ask for, and a string shorter than that comes back whole, without an error.
Additional Resources
- An Informal Introduction to Python: Text — indexing and slicing strings.
- Common sequence operations — the exact rules for
s[i:j].
