- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 String of Size N in Python
Suppose we have a string s and an integer n, we have to split the s into n-sized pieces.
So, if the input is like s = "abcdefghijklmn", n = 4, then the output will be ['abcd', 'efgh', 'ijkl', 'mn']
To solve this, we will follow these steps −
- i:= 0
- f:= a new list
- while i < size of s, do
- insert s[from index i to i+n-1] at the end of f
- i := i + n
- return f
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s, n): i=0 f= while(i<len(s)): f.append(s[i:i+n]) i+=n return(f) ob = Solution() print(ob.solve("abcdefghijklmn", 4))
['abcd', 'efgh', 'ijkl', 'mn']
- Program to rotate a string of size n, n times to left in Python
- Split tuple into groups of n in Python
- How to split a string in Python
- How to split string by a delimiter string in Python?
- How to split string on whitespace in Python?
- Break a list into chunks of size N in Python
- Split a string in equal parts (grouper in Python)
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Program to find number of ways to split a string in Python
- Python program to split and join a string?
- Python – Split Numeric String into K digit integers
- Python program to randomly create N Lists of K size
- How to get the size of a string in Python?