Python Forum
how to convert list into string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to convert list into string
#1
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
Reply
#2
>>> t = [1, 2, 3]
>>> t.insert(-1, 'test')
>>> t
[1, 2, 'test', 3]
Reply
#3
Thank you very much.
Reply
#4
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert every even number in a list to odd? Bruizeh 4 3,758 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Convert list of numbers to string of numbers kam_uk 5 3,004 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Convert multiple decimal numbers in string to floats Katy8 6 3,562 Aug-16-2020, 06:06 PM
Last Post: Katy8
  Convert a list of integers to strings? Cornelis 3 2,266 Nov-15-2019, 12:13 PM
Last Post: perfringo
  Error could not convert string to float: deadendstreet 4 5,397 Oct-02-2019, 05:49 PM
Last Post: ichabod801
  Convert string to a specific number of bytes artblinked 1 2,429 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  ValueError: could not convert string to float: 'Pencil' Balakay97 3 5,914 Mar-08-2018, 07:30 PM
Last Post: sparkz_alot
  How to convert boolean "True" to custom string? student8 3 12,592 Oct-01-2017, 06:24 AM
Last Post: hbknjr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020