paint-brush
How to Split String Every Nth Character in Pythonby@fedingo
4,402 reads
4,402 reads

How to Split String Every Nth Character in Python

by SreeramMay 31st, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Learn three different ways to split a string in Python at every Nth character. You can do this using list comprehensions, wrap() function or regular expressions.
featured image - How to Split String Every Nth Character in Python
Sreeram HackerNoon profile picture

Python provides many useful libraries and functions to work with strings. Sometimes, you may need to split a string at every Nth character. It can be tedious to loop through the string and extract substrings, one at a time. In this article, we will learn three simple ways to quickly split a string into substrings of N consecutive characters each.


Let us say you have the following string in Python.

s = '1234567890'

Let us say you want to split it after every two characters.

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

Here are three ways to split the string as per our requirement:

  1. Using List Comprehension
  2. Using Wrap Function
  3. Using Regular Expression


1. Using List Comprehension

In this approach, we use list comprehension along with indexes to get a list of substrings.

>>> S = '1234567890'
>>> n = 2
>>> [s[i:i+n] for i in range(0, len(s), n)]
['12', '34', '56', '78', '90']

In the above code, we use range() function in list comprehension, with three arguments. The first one is 0. The second argument is the length of the string. The third argument is the step n. In our case, n=2. So the range() function returns a list of [0, n, 2n, 3n,…]. We loop through this list, using its items as list indexes. In other words, we extract substrings from original string, between index i and i+n, where i takes the values 0, n, 2n, … from output of range() function.


You can include it in a function, as shown below.

def split_n(s, n):
  return [s[i:i+n] for i in range(0, len(s), n)]

S = '1234567890'
split_n(S, 2)

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

2. Using Wrap Function

Python comes with a built-in textwrap library that provides a wrap() function. It takes two arguments - string to be split and the number of characters to be present in each substring.

>>> from textwrap import wrap
>>> s = '1234567890'
>>> wrap(s, 2)
['12', '34', '56', '78', '90']

If the number of characters in the original string is not an exact multiple of N, then the last substring will contain all the remaining characters. For example, if your original string contains an odd number of characters and you want to split it into substrings of 2 characters each, then the last substring will contain only one character.

>>> from textwrap import wrap
>>> s = '123456789'
>>> wrap(s, 2)
['12', '34', '56', '78', '9']

Here’s a function to split a string using wrap()

from textwrap import wrap

def split_n(s, n):
  return wrap(s,n)

S = '1234567890'
split_n(S, 2)

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

3. Using Regular Expression

You can also use findall() function in Python’s regular expression library ‘re’ for this purpose.

>>> import re
>>> re.findall('..','1234567890')
['12', '34', '56', '78', '90']

In the above code, we use a regular expression consisting of 2 dots to specify a string with two characters.

If your original string has an odd number of characters and you need to split it into substrings of 2 characters each, then you can use the ‘..?’ regular expression to indicate that the second character is optional.

>>> import re
>>> re.findall('..?', '123456789')
['12', '34', '56', '78', '9']

Here is a function to split strings using regular expression.

import re

def split_n(s):
  return re.findall('..',s)

S = '1234567890'
split_n(S)

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

Conclusion

In this tutorial, we learned how to split a string in Python for every Nth character. Among them, using the wrap() function is the easiest method since it is readily available and works even in case N is a variable. You do not need to change your code when the value of N changes. On the other hand, using regular expressions allows you to specify complex substring patterns instead of blindly extracting substrings of N characters each. But in this case, you need to modify your regular expression if N changes in your requirement.