1

I am learning python and i'm trying to add 2 ** in front of each line of some example text. However when i call ljust(2,'*') on every element it doesn't change the original string. I want to replace the new element with this old string but how?

This is what i' ve tried. First i tried it with a regular for loop but that didn't work. Then i came across a question where list comprehensions are explained Perform a string operation for every element in a Python list so i tried that.

This is what i have now

example_string = '''hello there how are you doing!
i am doig well thank you
lets get to work!!! '''


def modify_example_string():
    global example_string
    new_string_list = [element.ljust(2,'*') for element in example_string.split('\n')]
    example_string = '\n'.join(new_string_list)
    print(new_string_list)

modify_example_string()

This should return a new list with all the ljust transformations but it doesn't so i would like to know the proper way of solving this.

FAM_Maurice
  • 363
  • 3
  • 10

3 Answers3

3

It seems like you misunderstood what ljust(2, '*') is doing. It does not add two * to the beginning of the string but will pad the string with * to a total length of 2. All your lines are longer, so it does nothing.

Instead, just use "**" + line to add the stars to the lines.

def modify_example_string():
    global example_string
    example_string = "\n".join("**" + line for line in example_string.splitlines())

Also, instead of using global I'd recommend using parameters and return values:

def prepend_stars(s):
    return "\n".join("**" + line for line in s.splitlines())

example_string = prepend_stars(example_string)
tobias_k
  • 81,265
  • 12
  • 120
  • 179
2

The ljust method doesn't do what you expect. The documentation says:

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

Your list comprehension is correct.

One solution might be to use format. A good tutorial here.

Example code with format:

example_string = '''hello there how are you doing!
i am doig well thank you
lets get to work!!! '''


def modify_example_string(example_string, ch, n):
    new_string_list = ["{} {}".format(ch * n, element)
                       for element in example_string.split('\n')]
    example_string = '\n'.join(new_string_list)
    return example_string

print(modify_example_string(example_string, "*", 2))
# ** hello there how are you doing!
# ** i am doig well thank you
# ** lets get to work!!!
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
0

I would split your string using the splitlines() method and use a for loop to iterate through the lines and build a new, concatenated string for output.

Something like this:

example_string = '''hello there how are you doing!
i am doig well thank you
lets get to work!!! '''


def modify_example_string(input_string):
    new_string_list = ''
    for line in input_string.splitlines():
        new_string_list += f'**{line}\n'
    return new_string_list

print(modify_example_string(example_string))
MurrayW
  • 393
  • 2
  • 10