Use a slice: s[:n] returns the first n positions of a string. For example, "Python"[:3] evaluates to "Pyt". The start is omitted, so it defaults to zero; the stop position is not included.
Use a slice to get the prefix
Put the string before the brackets and the number of characters you want after the colon:
text = "Python"
prefix = text[:3]
print(prefix) # Pyt
In s[start:stop], Python includes the start position and stops before the stop position. Leaving out start makes the slice begin at position zero. The official Python tutorial describes these defaults; the sequence operations documentation gives the corresponding slice behavior.
What happens at the boundaries
nis zero:s[:0]returns an empty string.nexceeds the string length: the slice returns the whole string; it does not raise an index error.nis negative: Python applies negative slice-bound rules. For example,s[:-1]returns all but the final code point, not a negative number of characters.
This makes a slice useful when the string may be shorter than the requested length: s[42] raises IndexError if that position is missing, while s[:42] safely stops at the string’s end. Slicing produces a new string value and does not alter the original; Python strings are immutable.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
What Python counts as a character
Python strings are sequences of Unicode code points, not encoded bytes. So s[:n] counts code-point positions. A visible character can consist of multiple code points—for example, a letter followed by a separate combining accent—so a slice can split a displayed character. Python slicing does not promise boundaries that match user-perceived grapheme clusters. These distinctions follow from Python’s Unicode documentation.
If you need a byte limit, choose an encoding and work with the encoded bytes deliberately; a string slice is not a byte limit. If you need to preserve visible grapheme clusters, code-point slicing alone may not meet that requirement.
Quick Recap
Best Value
Rank #2
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




