I have a string
ddffjh3gs
I want to convert it into a list
["ddf", "fjh", "3gs"]
In groups of 3 characters as seen above. What is the best way to do this in python 2.7?
As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.
split() only works with one argument, so I have all words with the punctuation after I split with whitespace.
Using list comprehension with string slice:
>>> s = 'ddffjh3gs'
>>> [s[i:i+3] for i in range(0, len(s), 3)]
['ddf', 'fjh', '3gs']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With