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
.