Python Forum
how to convert list into string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: how to convert list into string (/thread-18346.html)



how to convert list into string - Shevach - May-13-2019

I thought that this would be really easy but I'm having problems.
I want to print a list and add the word "and" before the last value

I tried with the .join method:
list=['a','b','c','d']
x=', '
print(x.join(list)))

This worked to print the list values as a string but I have no way to add the word "and".
How should I do this?
Thanks a lot
Shevach


RE: how to convert list into string - micseydel - May-13-2019

>>> t = [1, 2, 3]
>>> t.insert(-1, 'test')
>>> t
[1, 2, 'test', 3]



RE: how to convert list into string - Shevach - May-14-2019

Thank you very much.


RE: how to convert list into string - perfringo - May-14-2019

It is not good practice to use 'list' as name.

In [1]: list('abc')                                                                                    
Out[1]: ['a', 'b', 'c']

In [2]: list=['a','b','c','d']                                                                         

In [3]: list('abc')                                                                                    
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-88aa818ee054> in <module>
----> 1 list('abc')

TypeError: 'list' object is not callable