3

I'm very new to programming. I need to create a function that takes any string and a fill character and returns a list of 8 character strings. For example:

>>>segmentString("Hello, World!","-")
['Hello, W', 'orld!---']

As you can see, the string has 13 characters. The function splits into 2 strings, where the first contains 8 characters, and the second contains 5 characters and 3 fill characters.

So far, I've figured out how to do this for strings less than 8 characters, but not for more than 8 characters, where the string is split up, which is what I am stuck on.

def segmentString(s1,x):
    while len(s1)<8:
        s1+=x
    return s1

How do you split a string up?

hello
  • 63
  • 5

5 Answers5

3

You can use a slightly modified version of this chunking recipe:

def chunks(L, n, char):
    for i in range(0, len(L), n):
        yield L[i:i + n].ljust(n, char)

res = list(chunks('Hello, World!', 8, '-'))

['Hello, W', 'orld!---']

str.ljust performs the necessary padding.

jpp
  • 134,728
  • 29
  • 196
  • 240
  • 1
    Easier way to write the padding step: `yield x.ljust(n, char)`. No need to perform the calculations for required padding yourself over and over. And because it doesn't even need to know the original length of `x`, you could one-line the body of the loop to `yield L[i:i+n].ljust(n, char)` – ShadowRanger Nov 10 '18 at 01:56
1

A slightly modified version of @jpp's answer is to first pad the string with your fill character, so that the length is cleanly divisible by 8:

def segmentString(s, c):
    s = s + c * (8 - len(s) % 8)
    return [s[i:i+8] for i in range(0, len(s), 8)]

>>> segmentString("Hello, World!","-")
['Hello, W', 'orld!---']

And if you needed the size to be variable, you can just add in a size argument:

def segmentString(s, c, size):
    s = s + c * (size - len(s) % size)
    return [s[i:i+size] for i in range(0,len(s),size)]

>>> segmentString("Hello, World!","-",4)
['Hell', 'o, W', 'orld', '!---']
sacuL
  • 42,057
  • 8
  • 58
  • 83
1

Yet another possibility:

def segmentString(s1, x):
    ret_list = []
    while len(s1) > 8:
        ret_list.append(s1[:8])
        s1 = s1[8:]
    if len(s1) > 0:
        ret_list.append(s1 + x*(8-len(s1)))
    return ret_list
John Anderson
  • 21,424
  • 2
  • 11
  • 29
0

I am also new to programming but I think you could use this...

Word = "Hello, World!---"
print([Word[i:i+x] for i in range(0, len(Word), x)])

Just change x to whatever value you want.. Check out https://www.google.co.in/amp/s/www.geeksforgeeks.org/python-string-split/amp/ Hope it helps.

sacuL
  • 42,057
  • 8
  • 58
  • 83
  • I think that the issue is that the word is not `"Hello, World!---"`, but `"Hello, World!"`, and it needs to be split into 8 character chunks, with `-` as a filler when the last chunk is not 8 characters long – sacuL Nov 10 '18 at 01:53
0

Here is the simple answer that you can understand that solves your problem with few simple ifs:

a= 'abcdefghijklmdddn'

def segmentString(stuff):
    c=[]
    i=0
    hold=""

    for letter in stuff:
        i+=1
        hold+=letter

        if i%8==0:
            c.append(hold)
            hold=""

    c.append(hold)

    if hold=="":
        print(c)

    else:
        if hold==c[-1]:
            print(c)

        else:
            c.append(hold)
            print(c)