Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string into 2 in Python

Is there a way to split a string into 2 equal halves without using a loop in Python?

like image 944
Kiwie Teoh Avatar asked Jan 25 '11 03:01

Kiwie Teoh


People also ask

How do you cut a string into two parts?

Use the maxsplit parameter: a, b = s. split(maxsplit=1) .

How do you split one string in Python?

Python String | split() 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.


2 Answers

firstpart, secondpart = string[:len(string)/2], string[len(string)/2:] 
like image 52
Senthil Kumaran Avatar answered Sep 17 '22 20:09

Senthil Kumaran


a,b = given_str[:len(given_str)/2], given_str[len(given_str)/2:] 
like image 22
lalli Avatar answered Sep 19 '22 20:09

lalli