Split string into strings by length?

后端 未结 15 2087
半阙折子戏
半阙折子戏 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 07:00

    Got an re trick:

    In [28]: import re
    
    In [29]: x = "qwertyui"
    
    In [30]: [x for x in re.split(r'(\w{2})', x) if x]
    Out[30]: ['qw', 'er', 'ty', 'ui']
    

    Then be a func, it might looks like:

    def split(string, split_len):
        # Regex: `r'.{1}'` for example works for all characters
        regex = r'(.{%s})' % split_len
        return [x for x in re.split(regex, string) if x]
    
    0 讨论(0)
  • 2020-11-27 07:02

    In Split string every nth character?, "the wolf" gives the most concise answer:

    >>> import re
    >>> re.findall('..','1234567890')
    ['12', '34', '56', '78', '90']
    
    0 讨论(0)
  • 2020-11-27 07:06
    • :param s: str; source string
    • :param w: int; width to split on

    Using the textwrap module:

    PyDocs-textwrap

    import textwrap
    def wrap(s, w):
        return textwrap.fill(s, w)
    

    :return str:

    Inspired by Alexander's Answer

    PyDocs-data structures

    def wrap(s, w):
        return [s[i:i + w] for i in range(0, len(s), w)]
    
    • :return list:

    Inspired by Eric's answer

    PyDocs-regex

    import re
    def wrap(s, w):    
        sre = re.compile(rf'(.{{{w}}})')
        return [x for x in re.split(sre, s) if x]
    
    • :return list:

    Complete Code Examples/Alternative Methods

    0 讨论(0)
  • 2020-11-27 07:07

    The string splitting is required in many cases like where you have to sort the characters of the string given, replacing a character with an another character etc. But all these operations can be performed with the following mentioned string splitting methods.

    The string splitting can be done in two ways:

    1. Slicing the given string based on the length of split.

    2. Converting the given string to a list with list(str) function, where characters of the string breakdown to form the the elements of a list. Then do the required operation and join them with 'specified character between the characters of the original string'.join(list) to get a new processed string.

    0 讨论(0)
  • 2020-11-27 07:07
    l = 'abcdefghijklmn'
    
    def group(l,n):
        tmp = len(l)%n
        zipped = zip(*[iter(l)]*n)
        return zipped if tmp == 0 else zipped+[tuple(l[-tmp:])]
    
    print group(l,3)
    
    0 讨论(0)
  • 2020-11-27 07:08

    My solution

       st =' abs de fdgh  1234 556 shg shshh'
       print st
    
       def splitStringMax( si, limit):
        ls = si.split()
        lo=[]
        st=''
        ln=len(ls)
        if ln==1:
            return [si]
        i=0
        for l in ls:
            st+=l
            i+=1
            if i <ln:
                lk=len(ls[i])
                if (len(st))+1+lk < limit:
                    st+=' '
                    continue
            lo.append(st);st=''
        return lo
    
       ############################
    
       print  splitStringMax(st,7)
       # ['abs de', 'fdgh', '1234', '556', 'shg', 'shshh']
        print  splitStringMax(st,12)
    
       # ['abs de fdgh', '1234 556', 'shg shshh']
    
    0 讨论(0)
提交回复
热议问题