-1

I am making a poem generator in python, and I am currently working on how poems are outputted to the user. I would like to make it so every line that is outputted will have a comma follow after. I thought I could achieve this easily by using the .join function, but it seems to attach to letters rather than the end of the string stored in the list.

line1[-1]=', '.join(line1[-1])
print(*line1)
print(*line2)

Will output something like:

Moonlight is w, i, n, t, e, r
The waters scent fallen snow

In hindsight, I should have known join was the wrong function to use, but I'm still lost. I tried .append, but as the items in my list are strings, I get the error message "'str' object has no attribute 'append'."

I realize another fix to all this might be something like:

print(*line1, ",")

But I'm working on a function that will decide whether the correct punctuation needs to be "!", ",", "?", or "--", so I am really hoping to find something that can be attached to the string in the list itself, instead of tacked on during the output printing process.

  • 1
    Could you add your code? – Mert Köklü Nov 10 '19 at 18:09
  • 1
    Something like `line = '{},'.format(line)` ought to work - or even `line = line + ","` – snakecharmerb Nov 10 '19 at 18:10
  • Run this: `print(‘hi’ + ‘!’)` – AMC Nov 10 '19 at 18:11
  • From the [docs](https://docs.python.org/2/library/stdtypes.html#str.join), join will concatenate a given iterable object inserting the string on which it's called in between. So in your example, I'm guessing your `line1` is a list of words, and you are joining the characters of the last word (`line1[-1]`) using the comma. If you are using list of words (which matches the usage of `*` when printing it), just append the comma to that list of words. – berna1111 Nov 10 '19 at 18:22

2 Answers2

1

Just use the + or += operator for strings, for example:

trailing_punct = ',' # can be '!', '?', etc.

line1 += trailing_punct
# or
line1 = line1 + trailing_punct

+= can be used to modify the string "in place" (note that under the covers, it does create a new object and assign to it, so id(line1) will have changed after this operation).

smarchese
  • 510
  • 2
  • 8
0

It seems your line1 and line2are lists of strings, so I'll start by assuming that:

line1 = ["Moonlight", "is", "winter"]
line2 = ["The", "waters", "scent", "fallen", "snow"]

You are using the default behaviour of the print function when given several string arguments to add the space between words: print(*line1) is equivalent to calling print(line1[0], line1[1], ...) (see *args and **kwargs).

That makes adding the line separator to the list of words of the line insufficient, as it will have a space before it:

print("\n--//--\nUsing print default space between given arguments:")
line_separator = ","
line1.append(line_separator)
print(*line1)
print(*line2)

Results in:

--//--
Using print default space between given arguments:
Moonlight is winter ,
The waters scent fallen snow

What you want to do can be done by joining the list of words into a single string, and then joining the list of lines with the separator you want:

print("\n--//--\nPrinting a single string:")
line1_str = ' '.join(line1)
line2_str = ' '.join(line2)
line_separator = ",\n"  # notice the new line \n
lines = line_separator.join([line1_str, line2_str])
print(lines)

Results in

--//--
Printing a single string:
Moonlight is winter,
The waters scent fallen snow

Consider using a list of lines for easier expansion, and maybe a list of separators to be used in order for each line.

berna1111
  • 1,811
  • 1
  • 18
  • 23