Splitting a string into groups of three characters

John Machin sjmachin at lexicon.net
Mon Aug 8 18:29:35 EDT 2005


William Park wrote:
> lemon97 at gmail.com <lemon97 at gmail.com> wrote:
> 
>>Hi,
>>
>>Is there a function that split a string into groups, containing an "x"
>>amount of characters?
>>
>>Ex.
>>TheFunction("Hello World",3)
>>
>>Returns:
>>
>>['Hell','o W','orl','d']
>>
>>
>>Any reply would be truly appreciated.
> 
> 
> Look into 're' module.  Essentially, split using '...', but return the
> separator as well.  Then, remove empty items.  In Bash shell, you would

This would have to be a candidate for arcane side-effect of the month.
Not in front of the children, please.

> do
>     a="Hello World"
>     set -- "${a|?...}"		# extract '...' separators
>     pp_collapse			# remove null items
>     printf '{%s}\n' "${@}"
> 
> Translating to Python is left as homework.

Given that the OP was struggling to write a simple loop in Python, 
giving him an answer in hieroglypghics and suggesting he translate it 
into Python could be described as unhelpful.



More information about the Python-list mailing list