2

I have a tuple of strings like this:

stringTuple = ['String1', 'String2', 'String3', 'String4', 'String5', 'String6']

I later combine this tuple with multiple others and input into an excel document. I need new line break between each of these tuples - so I need it to look like this.

stringTuple = ['String1', 'String2', 'String3', 'String4', 'String5', 'String6\n']

I've looked into different sorts of string concatenation such as: stringTuple[-1:] = stringTuple[-1:] + '\n' with no luck

petezurich
  • 9,280
  • 9
  • 43
  • 57
babis95
  • 582
  • 6
  • 29

2 Answers2

2

The last element in your list is stringTuple[-1].

Add a line break to the last element in your list:

stringTuple[-1] = stringTuple[-1] + "\n"

Or just:

stringTuple[-1] += "\n"

By the way, stringTuple[-1:] is a slice of your array (an interesting read is in another SO question, Understanding slice notation). stringTuple[start:] yields a list of all the items in a list, from index start onwards. In this case, stringTuple[-1:] is a list of all your items from the last index onwards (i.e. a list of the last item in your original list):

stringTuple = ['String1', 'String2', 'String3', 'String4', 'String5', 'String6']

print(stringTuple[-1:]) # ['String6']

Making this work with tuples

Tuples are immutable, so you can't make modifications in place (if you want to modify items in a tuple, you actually need to create a new tuple with the modified items in it):

stringTuple = ('String1', 'String2', 'String3', 'String4', 'String5', 'String6')

# get last item in tuple, add line break to it
lastItemWithLineBreak = stringTuple[-1] + "\n"

# create a new tuple consisting of every item in our original list but the last
# and then add our new modified item to the end
newTuple = tuple(i for i in stringTuple[:-1]) + (lastItemWithLineBreak,)

print(newTuple) # ('String1', 'String2', 'String3', 'String4', 'String5', 'String6\n')

Note the special notation (lastItemWithLineBreak,), which is used to create a tuple consisting of the single element lastItemWithLineBreak.

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
  • 1
    Thank you for your answer. Your solution works great with a list. The List i have is of type: tuple. Which after running your code produces this error: TypeError: 'tuple' object does not support item assignment – babis95 Jan 16 '20 at 11:46
  • Tuples are immutable which means they can't be modified. You'd need to create a new tuple consisting of the items you want to keep plus the new last item – Ismael Padilla Jan 16 '20 at 11:47
  • @babis95 I edited my answer to include an exampe of how to make this work with tuples – Ismael Padilla Jan 16 '20 at 11:54
1

Simple solution, add end line for last element of tuple:

stringTuple[-1] += '\n'
isydmr
  • 649
  • 7
  • 16
  • Thank you for your answer. Your solution works great with a list. The List i have is of type: tuple. Which after running your code produces this error: TypeError: 'tuple' object does not support item assignment – babis95 Jan 16 '20 at 11:46