Split a string into 2 in Python

前端 未结 6 826
轻奢々
轻奢々 2020-12-29 21:42

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

6条回答
  •  梦谈多话
    2020-12-29 22:22

    minor correction the above solution for below string will throw an error

    string = '1116833058840293381'
    firstpart, secondpart = string[:len(string)/2], string[len(string)/2:]
    

    you can do an int(len(string)/2) to get the correct answer.

    firstpart, secondpart = string[:int(len(string)/2)], string[int(len(string)/2):]

提交回复
热议问题