Split a string into 2 in Python

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

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

6条回答
  •  梦毁少年i
    2020-12-29 22:16

    In Python 3:
    If you want something like
    madam => ma d am
    maam => ma am

    first_half  = s[0:len(s)//2]
    second_half = s[len(s)//2 if len(s)%2 == 0 else ((len(s)//2)+1):]
    

提交回复
热议问题