How to split string based on number in python

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.

s = 'ABCDEFG'

n = 2
res = [s[i:i + n] for i in range[0, len[s], n]]

print[res]

Output:

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"
parts = s.split[" "]
print[parts]

Result:

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."
parts = s.split["."]
print[parts]

This will result in:
['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 like words = 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

How do you split a string in Python with numbers?

Python String split[] Method The split[] method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Can we divide string by number?

Examples: Input : 1234 Output : Possible 1 Explanation: String can be split as "1", "2", "3", "4" Input : 99100 Output :Possible 99 Explanation: String can be split as "99", "100" Input : 101103 Output : Not Possible Explanation: It is not possible to split this string under given constraint.

How do you split a string between letters and digits in Python?

The Python standard library comes with a function for splitting strings: the split[] function. This function can be used to split strings between characters. The split[] function takes two parameters. The first is called the separator and it determines which character is used to split the string.

How do you split a string at a specific position in Python?

Python String split[] Method Syntax separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator. maxsplit : It is a number, which tells us to split the string into maximum of provided number of times.

Bài mới nhất

Chủ Đề