Split a string into 2 in Python

前端 未结 6 824
轻奢々
轻奢々 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:29

    Whoever is suggesting string[:len(string)/2], string[len(string)/2 is not keeping odd length strings in mind!

    This works perfectly. Verified on edx.

    first_half = s[:len(s)//2]

    second_half = s[len(s)//2:]

提交回复
热议问题