
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Split a String by Custom Lengths in Python
In this article, we will learn how to split a string by custom lengths in python.
Methods Used
The following are the various methods to accomplish this task ?
Using for loop and slicing
Using List Comprehension, join() and next() functions
Example
Assume we have taken an input string and custom lengths list. We will now split an input string by input custom lengths using the above methods.
Input
inputString = 'hitutorialspoint' customLengths = [4, 1, 6, 5]
Output
['hitu', 't', 'orials', 'point']
In the above example, firstly the input list is split based on the given custom lengths ?
4 characters - 'hitu'
1 character - ?r'
6 characters - ?orials'
5 'characters - ?point
Method 1: Using for loop and slicing
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task -.
Create a variable to store an input string.
Print the input string.
Create a variable to store an input custom lengths list.
Create an empty list for storing the resultant list.
Initialize the start index as 0.
Use the for loop to traverse through each element of the custom lengths list.
Use the append() function(adds the element to the list at the end) to append the result that is sliced between the given indices.
Increment the value of the start index with the custom length list element.
Print the resultant list after splitting the input list by given custom lengths.
Example
The following program returns a list after splitting the input list by given custom lengths using for loop and slicing -
# input string inputString = 'hitutorialspoint' # printing input string print("Input string: ", inputString) # input custom lengths list customLengths = [4, 1, 6, 5] # empty list for storing a resultant list outputList = [] # initializing start index as 0 startIndex = 0 # travsering through each element of the custom lengths list for l in customLengths: # appending the custom length string sliced from the # starting index to the custom length element outputList.append(inputString[startIndex: startIndex + l]) # Increment the start Index value with the custom length element startIndex += l # printing the resultant output list print("Resultant list after splitting by custom lengths:\n", outputList)
Output
On executing, the above program will generate the following output -
Input string: hitutorialspoint Resultant list after splitting by custom lengths: ['hitu', 't', 'orials', 'point']
Method 2: Using List Comprehension, join() and next() functions
List Comprehension
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
next() function
Returns the next item in an iterator like a list, string tuple, etc. If the iterable reaches its end, you can add a default return value to return.
Syntax
next(iterable, default)
join() function ? join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.
iter() function
Returns an iterator for the given object. It creates an object that iterates one element at a time.
Syntax
Below is the syntax of the function.
iter(object, sentinel)
Parameters
object ? The object whose iterator(list, tuples, sets etc) to be created is specified by this parameter
sentinel ? It is a unique value that indicates the end of a sequence.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -
Use the iter() function to return an iterator for the input string(iterating through string)
Traverse through each element of customLengths list and Join the next l(iterator value) characters of the string using the next() and join() function.
Print the resultant list after splitting the input list by given custom lengths.
Example
The following program returns a list after splitting the input list by given custom lengths using list comprehension, join(), and next() functions -
# input string inputString = 'hitutorialspoint' # printing input string print("Input string: ", inputString) # input custom lengths list customLengths = [4, 1, 6, 5] # Getting the string as an iter object to iterate through it stringitr = iter(inputString) # traversing through each element of customLengths list and # Joining the next l characters of the string outputList = ["".join(next(stringitr) for i in range(l)) for l in customLengths] # printing the resultant output list print("Resultant list after splitting by custom lengths:\n", outputList)
Output
On executing, the above program will generate the following output -
Input string: hitutorialspoint Resultant list after splitting by custom lengths: ['hitu', 't', 'orials', 'point']
Conclusion
In this article, we learned how to split the given string into different lengths using 2 different methods. Additionally, we learned how to use the iter() method to iterate through the string and the next() function to get the string up to n characters.