Home python How to make a formatted output list?

How to make a formatted output list?




I can not understand one error:

TypeError: can only concatenate list (not “str”) to list

There is a line in which the item is added to the various and later can display an item (before the last must be and ):

a = []
print ( "input")
While True:
  name = input ()
  if name == "":
  a = a + [name]
print (a [: - 1] + "and" + a [-1])

Tried been administered str for prints separate element, out through the list (()) , tuple ([]) , but then get here’s the answer:

[ ‘123’, ‘456’] and 789.

I need a result of this, but without the [] these brackets.

Answer 1, Authority 100%

a [: - 1] + "and" + a [-1]

and in your list, a [: - 1] – section of the list (ie the list), “and” – line, a [ -1] – the last item in the list, ie, also line

Well, as you list with 2 lines add up?

If you enter a few lines and you want them to be converted into a string, where the last entry is separated from the rest by “and”, then this should be on the other

for example in your code need to turn in a list


'' .join (a [: - 1]) + "and" + a [-1]

through the method join you can combine all the strings in the list into a string with a separator

In this example, separated by a space

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions