Warning: session_start(): open(/tmp/sess_2549a36101342601444f61da4ef2a8e0, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Split A String Every Nth Character In Python? - LearnShareIT

How To Split A String Every Nth Character In Python?

Split a string every nth character in Python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *