In this article, you will learn how to split a string at every Nth character in python. There are many ways to split a string at every Nth character.
Here are some examples of how to split a string at every Nth Character
In this example, we used the list comprehension for a more compact implementation.
# Define a Function
def splitString(n,str):
splittedString = [str[i:i+n] for i in range(0, len(str), n)]
return splittedString
# output
print(splitString(2,'Tutorialsrack.com'))
# Output => ['Tu', 'to', 'ri', 'al', 'sr', 'ac', 'k.', 'co', 'm']
print(splitString(3,'Tutorialsrack.com'))
# Output => ['Tut', 'ori', 'als', 'rac', 'k.c', 'om']
print(splitString(2,'1234567890123'))
# Output => ['12', '34', '56', '78', '90', '12', '3']
In this example, we used the regular expression to split a string at every Nth character in python. And we used the findall()
method from the re
module to split a string at every Nth character in python.
# Import Module
import re
# Define a Function
def splitString(n,str):
splittedString = re.findall(n,str)
return splittedString
# output
print(splitString('..?','Tutorialsrack.com'))
# Output => ['Tu', 'to', 'ri', 'al', 'sr', 'ac', 'k.', 'co', 'm']
print(splitString('.{1,2}','Tutorialsrack.com'))
# Output => ['Tu', 'to', 'ri', 'al', 'sr', 'ac', 'k.', 'co', 'm']
print(splitString('..?','1234567890123'))
# Output => ['12', '34', '56', '78', '90', '12', '3']
print(splitString('.{1,2}','1234567890123'))
# Output => ['12', '34', '56', '78', '90', '12', '3']
In this example, we used the wrap()
method from the textwrap
module.
# Import Module
from textwrap import wrap
# Define a Function
def splitString(n, str):
splittedString = wrap(str,n)
return splittedString
# output
print(splitString(2,'Tutorialsrack.com'))
# Output => ['Tu', 'to', 'ri', 'al', 'sr', 'ac', 'k.', 'co', 'm']
print(splitString(3,'Tutorialsrack.com'))
# Output => ['Tut', 'ori', 'als', 'rac', 'k.c', 'om']
print(splitString(2,'1234567890123'))
# Output => ['12', '34', '56', '78', '90', '12', '3']
print(splitString(4,'1234567890123'))
# Output => ['1234', '5678', '9012', '3']
In this example, we used a
for
loop and range(start, stop, step)
to iterate over a range from start to stop where the stop is the length of the string len(string)
and step is every number of characters where the string will be split. Use string slicing syntax string[index : index + step]
to acquire a string with step characters. Use list.append()
function to add that previously described string to a list.
# Define a Function
def splitString(n,str):
splittedString = []
for index in range(0, len(str), n):
splittedString.append(str[index : index + n])
# Return Output
return splittedString
# output
print(splitString(2,'Tutorialsrack.com'))
# Output => ['Tu', 'to', 'ri', 'al', 'sr', 'ac', 'k.', 'co', 'm']
print(splitString(3,'Tutorialsrack.com'))
# Output => ['Tut', 'ori', 'als', 'rac', 'k.c', 'om']
print(splitString(2,'1234567890123'))
# Output => ['12', '34', '56', '78', '90', '12', '3']
print(splitString(4,'1234567890123'))
# Output => ['1234', '5678', '9012', '3']
I hope this article will help you to understand how to split a string at every Nth character in python.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments