0 votes
1 view
in Python by (47.8k points)

Is it possible to split a python string every nth character?

For example, suppose I have a string containing the following:

'1234567890'

How can I get it to look like this:

['12','34','56','78','90']

1 Answer

0 votes
by (107k points)

There are many ways by which you can split string’s every nth character some of the import methods are as follows:-

The first method you can use to solve this problem is by using the len() function.

abc= '1234567890'

n = 2

[abc[i:i+n] for i in range(0, len(abc), n)]

image

The second way of solving this problem is by using the regular expression module and its findall() function below is the code that shows how we can use the regular expressions to split string by nth char:-

import re

re.findall('..','1234567890')

image

Related questions

0 votes
1 answer
asked Jul 31, 2019 in Python by Rajesh Malhotra (17.2k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 4, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...