1

I'm running into a problem which I cannot solve online- all answers I've found only allow the appending to happen once since it just keeps repeating the same action.

For context: If a string isn't 128 lines long- I want to pad it out to reach 128. All padding should add 00 then move to the next line. For example:

01
01
02
03
05
06
09
01

Then with padding should become

01
01
02
03
05
06
09
01
00
00
00
00 UP TO 128 lines

Hope that explains what I need to do.

I've tried using .join and .ljust/.rjust. inside a while loop. The while loop is:

while count != 129:
     padding.join("00\n")
     count += 1

However it only ever prints out 00. Any advice is appreciated. Thank you!

3
  • 1
    Is using a while loop strictly required here as there's other and more practical methods to do this... Commented Jul 8, 2022 at 11:07
  • In your own words, where the code says padding.join("00\n"), exactly what do you expect this to do, and why? What does the name padding mean in your program? What do you expect should be contained in padding after the loop, and how would this help you solve the problem? What is contained in padding after the loop? Please read ericlippert.com/2014/03/05/how-to-debug-small-programs. Commented Jul 8, 2022 at 11:13
  • "Any advice is appreciated. Thank you!" This is not answerable. Please try to come to your best possible understanding of what is going wrong, and then ask a specific question about that. It should start with a question word like "why" or "how", and end with a question mark (?). Commented Jul 8, 2022 at 11:15

2 Answers 2

1
your_string = "01\n01\n02\n03\n05\n06\n09\n01\n"
new_string =  your_string + (128 - len(your_string.split())) * "01\n"
Sign up to request clarification or add additional context in comments.

Comments

1

In order to check the number of lines you need to count the number of "\n" occurrences. Since the string seems to be a variable amount you need to be able to do this dynamically. You would have to write a function to check this.

something like this should work

def pad_string(unpadded_string, string_length =128, pad_string ='00\n'):
    """Function to ensure that a string is ``string_length`` lines.
    Do this by counting the number of new lines and appending deliminator where neccesary"""
    
    num_lines = unpadded_string.count('\n')
    if not num_lines < string_length:
        return unpadded_string
    return unpadded_string + pad_string*(string_length-num_lines)

Using this in your example:

your_string = "01\n01\n02\n03\n05\n06\n09\n01\n"


def pad_string(unpadded_string, string_length =128, pad_string ='00\n'):
    """Function to ensure that a string is ``string_length`` lines.
    Do this by counting the number of new lines and appending deliminator where neccesary"""
    
    num_lines = unpadded_string.count('\n')
    if not num_lines < string_length:
        return unpadded_string
    return unpadded_string + pad_string*(string_length-num_lines)


print(pad_string(your_string).count('\n'))
>>> 128

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.