Split string into strings by length?

后端 未结 15 2190
半阙折子戏
半阙折子戏 2020-11-27 06:31

Is there a way to take a string that is 4*x characters long, and cut it into 4 strings, each x characters long, without knowing the length of the s

15条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 06:51

    # spliting a string by the length of the string
    
    def len_split(string,sub_string):
        n,sub,str1=list(string),len(sub_string),')/^0*/-'
        for i in range(sub,len(n)+((len(n)-1)//sub),sub+1):
            n.insert(i,str1)   
        n="".join(n)
        n=n.split(str1)
        return n
    
    x="divyansh_looking_for_intership_actively_contact_Me_here"
    sub="four"
    print(len_split(x,sub))
    
    # Result-> ['divy', 'ansh', 'tiwa', 'ri_l', 'ooki', 'ng_f', 'or_i', 'nter', 'ship', '_con', 'tact', '_Me_', 'here']
    

提交回复
热议问题