Use range[] function and slicing notation to split string by a number of characters in Python. Simple example code split a string into array every 2 characters python. Output:
s = 'ABCDEFG'
n = 2
res = [s[i:i + n] for i in range[0, len[s], n]]
print[res]
Same example using list comprehension
import math
s = 'ABCDEFG'
chunks, chunk_size = len[s], math.ceil[len[s] / 4]
res = [s[i:i + chunk_size] for i in range[0, chunks, chunk_size]]
print[res]
OR
s = '1234567890'
n = 2
res = [s[i:i+n] for i in range[0, len[s], n]]
print[res]
Using regex
import re
res = re.findall['..', '1234567890']
print[res]
Output: [’12’, ’34’, ’56’, ’78’, ’90’]
Do comment if you have any doubts and suggestions on this Python split topic.
Note: IDE: PyCharm 2021.3.3 [Community Edition]
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.
In Python you can split a string with the split[]
method. It breaks up a string [based on the given separator] and returns a list of strings.
To split a string, we use the method .split[]
. This method will return one or more new strings. All substrings are returned in a newly created list.
Note: Split with the method str.split[sep, maxsplit]. The 2nd parameter is optional. If maxsplit is set, it returns no more than maxsplit elements.
Related course: Complete Python Programming Course & Exercises
Introduction
The split function breaks a string down into smaller chunks [a list of strings]. Splitting a string is the opposite of concatenation [merges multiple strings into one string].
The split[] method takes maximum of 2 parameters, of which one is optional. The syntax of the split[] function is:
str.split[[separator [, maxsplit]]]
The first parameter seperator
is the string to split on, this could be a space, a comma, a dash, a word etcetera.
The second parameter maxsplit
is the maximum number of splits. By default there is no maximum and most programs don’t require a maximum number of splits.
String split example
We can split a string based on a character " "
, this is named the seperator.
s = "To convert the result to"Result:
parts = s.split[" "]
print[parts]
Any character [seperator] can be used. If you want to get sentences you could use:
s = "Python string example. We split it using the dot character."This will result in:
parts = s.split["."]
print[parts]
['Python string example', ' We split it using the dot character', '']
It’s not limited to a space, you can seperate on a comma:
>>> s = "alice,allison,chicago,usa,accountant"
>>> s = s.split[","]
>>> s
['alice', 'allison', 'chicago', 'usa', 'accountant']
>>>
You can split on a dot [turning texts into individual sentences]
>>> s = "This is an example sentence. It has several words, and is in the English language. It's dummy data for Python."
>>> s = s.split["."]
>>> s
['This is an example sentence', ' It has several words, and is in the English language', " It's dummy data for Python", '']
>>>
The seperator can be a word instead of a single symbol or character
s = s.split["is"]
s = s.split["python"]
Note that we overwrite s, when calling the s.split[] function. This is optional, if you want to keep the string you can store the result in a new variable.
Something likewords = s.split[]
If no separator is defined if you call the split[] function, it will use a whitespace by default.
>>> s = "hello world how are you"
>>> print[s.split[]]
['hello', 'world', 'how', 'are', 'you']
>>>
Related course: Complete Python Programming Course & Exercises