Use a range that advances by n and slice the string at each position. This produces fixed-width chunks, with a shorter final chunk when the string length is not evenly divisible by n.
Split a string into fixed-width chunks
Python string slicing gives a concise solution:
def chunk_string(s: str, n: int) -> list[str]:
if n <= 0:
raise ValueError("n must be positive")
return [s[i:i + n] for i in range(0, len(s), n)]
print(chunk_string("abcdefgh", 3))
# ['abc', 'def', 'gh']
range(0, len(s), n) generates starting offsets separated by n. Each slice, s[i:i + n], takes up to the next n characters. Python slicing stops at the end of the string, so the final chunk can be shorter. The Python sequence operations documentation describes slicing and sequence behavior.
As an Amazon Associate I earn from qualifying purchases.
Input and edge cases
nmust be positive. The function raisesValueErrorfor zero or negative values, which do not represent forward chunk sizes.- An empty string returns an empty list. There are no offsets to iterate over.
- A remainder becomes the last chunk. For example, splitting eight characters into chunks of three yields two three-character chunks and one two-character chunk.
Use a generator for incremental processing
The list-comprehension version builds and retains every chunk. If you want to process chunks one at a time, yield them instead:
def iter_chunks(s: str, n: int):
if n <= 0:
raise ValueError("n must be positive")
for i in range(0, len(s), n):
yield s[i:i + n]
for chunk in iter_chunks("abcdefgh", 3):
print(chunk)
This returns an iterator rather than a list; each chunk is produced as the loop requests it. The same positive-size check applies.
#1 Best Overall
Choose the method that matches the boundary you need
| Method | What determines each boundary? | Result behavior |
|---|---|---|
| String slicing with stepped offsets | Exactly n Python string elements |
Fixed-width chunks, except a possibly shorter final chunk; use a list comprehension or generator |
textwrap.wrap(s, width=n) |
Text formatting, including words and whitespace | Wrapped lines; not guaranteed to be exact fixed-width slices |
str.split(sep) or str.split() |
A specified delimiter, or runs of whitespace when no separator is given | Fields separated by those delimiters, not chunks of every n characters |
Use textwrap.wrap when the goal is readable lines. Its wrapping options and behavior are documented in Python’s textwrap documentation. Use str.split for delimiter-separated fields or whitespace-separated words, as described in the string methods documentation. For exact character-count groups, use slicing.
Know what Python counts as a character
The slicing examples divide a Python string by its string elements; they do not divide encoded data into fixed-size UTF-8 byte blocks. To chunk by bytes, encode the text first and slice the resulting bytes object.
Rank #2
String slicing also does not guarantee that each chunk is a visible character as a person perceives it. Some accented letters and emoji are formed from multiple Unicode code points, and a slice can split such a sequence. If chunks must preserve visible grapheme clusters, use a grapheme-aware library rather than ordinary slicing.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
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.




