Skip to main content

How to Get the First Characters of a String in Python with Slicing

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<string>[:<count>]
PartWhat 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​

first_characters.py
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].

  • 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​

Terminal
uv run python first_characters.py
Output
2026-12-03
2026
Hi

The terminal after uv run python first_characters.py: 2026-12-03, then 2026, then 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​

MistakeWhat happensDo this instead
Using an index instead of a slice: timestamp[10]You get one character, T, the one at position 10Add the colon: timestamp[:10]
Slicing a datetime object instead of its textTypeError: 'datetime.datetime' object is not subscriptableTurn it into text first with .isoformat(), or use .date()
Counting from 1: timestamp[:9] for 10 charactersYou get one character too fewThe number after the colon is how many characters you get

Troubleshooting​

SymptomLikely causeFix
TypeError: '...' object is not subscriptableThe value isn't a stringCheck its type with type(value) and convert it to text first
The slice is shorter than asked forThe string itself is shorterExpected: 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​