-6

I want to append a string with a tab character to a list. Then, when I print the list, I want to be able to get the same output when I do the print(string).

For example:

list1 = []
string = "Names:\tLucas"
print(list1)
print(string)

The output:

['Names\tLucas']
Names   Lucas

What's a way to do this.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 2
    What exact output are you looking for? Your question is quite unclear. – ahmadjanan Jan 21 '21 at 22:22
  • Read the [documentation](https://docs.python.org/3/tutorial/datastructures.html#data-structures). The first method is `.append`. The code posted is broken and the output is incorrect for the code shown. – Mark Tolonen Jan 21 '21 at 22:24
  • 1
    When I do print list, I want to be able to get the same output when I do the print(string) – Lucas Sun Jan 21 '21 at 23:10
  • What does that have to do with the title question? Edit the question to make it reproducible and clear what you want, or delete it. – Mark Tolonen Jan 22 '21 at 01:57

2 Answers2

0

You will have to print the list items themselves, instead of the list.

By iteration:

>>> list1 = []
>>> string1 = "Names:\tLucas"
>>> string2 = "Names:\tJohn"
>>>
>>> list1.append(string1)
>>> list1.append(string2)
>>> 
>>> for item in list1:
...   print(item)
... 
Names:  Lucas
Names:  John

By unpacking the list:

>>> list1 = []
>>> string1 = "Names:\tLucas"
>>> string2 = "Names:\tJohn"
>>>
>>> list1.append(string1)
>>> list1.append(string2)
>>>
>>> print(*list1, sep='\n')
Names:  Lucas
Names:  John
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
-2

try something like this

print("{}".format(string))
jmnguye
  • 347
  • 2
  • 9