If you are having some confusion about split a string every nth character in Python, take a look at our tutorial below to get the answer.
Split a string every nth character in Python
Traverse the string and slice it
You can traverse a string by loop and slice it to split the string of every nth character in Python.
Traverse with for loop:
In this example, we will loop the string by for and range() function with a step equal to n as an n-stage jump. After each jump, we will put the sub-string on another list. All the steps are displayed below:
- Step 1: Create an empty list to store the sub-string
- Step 2: Traverse the string by for loop and range() function
- Step 3: Append the list with an interval equal to n
Code:
myString = "Welcome_to_Learn_Share_IT" mySubString = [] n = 3 for i in range(0, len(myString), n): mySubString.append(myString[i:i+n]) print(mySubString)
Note: myString[i:i+n] means elements from index i to index i+n-1
Result:
['Wel', 'com', 'e_t', 'o_L', 'ear', 'n_S', 'har', 'e_I', 'T']
Traverse with the while loop:
The idea of this example is the same as using for loop. The only difference is that you use the while loop and update the initial string after each stage. All the steps are demonstrated below:
- Step 1: Create an empty list to store sub-strings
- Step 2: Traverse the string by the while loop
- Step 3: Append the list with a sub-string equal to n and update the string without the sub-string simultaneously
Code:
myString = "Welcome_to_Learn_Share_IT" mySubString = [] n = 3 while myString: mySubString.append(myString[:n]) myString = myString[n:] print(mySubString)
Note: myString[n:] means element from index n to the rest of myString
Result:
['Wel', 'com', 'e_t', 'o_L', 'ear', 'n_S', 'har', 'e_I', 'T']
Use wrap() function
In this method, we use a more straightforward way to split the string, and it is the wrap() function. In Python, the textwrap() module provides some convenient functions to work with text data. You can use this module’s wrap() function to split the string into sub-strings.
Syntax:
wrap(selected_string, length-n)
Parameter:
- selected_string: The string needed to split
- Length-n: The length of expected sub-string
Code:
from textwrap import wrap myString = "Welcome_to_Learn_Share_IT" mySubString = [] n = 3 # Use wrap() function to split myString into mySubString mySubString = wrap(myString,n) print(mySubString)
Result:
['Wel', 'com', 'e_t', 'o_L', 'ear', 'n_S', 'har', 'e_I', 'T']
Summary
You have been through our article to split a string every nth character in Python, I hope you understand the ideas to put into practice effectively. Visit our website for more knowledge, and let us know if you have any questions.
Maybe you are interested:
- Split a string by whitespace in python
- Split a string with multiple delimiters in python
- Find index of last occurrence of substring in Python String
My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP