The following works when I know that sample_list
will hold exactly 4 items.
sample_list = ['cat', 'dog', 'bunny', 'pig']
print("Your list of animals are: {}, {}, {} and {}".format(*sample_list))
How could I format the string if I do not know the number of items that sample_list
will contain at run time? This would mean that I can't enter the appropriate number of bracket place holders at design time.
join
all but the last element with commas, thenjoin
the last word onto the string with "and".len(sample_list)
.