2

Is there a way to combine all items in a list into one string?

    print(isbnl)# str(isbnl).strip('[]'))
user3849501
  • 81
  • 1
  • 1
  • 4
  • 7
    You mean `str.join()`? e.g. `', '.join(isbnl)`? You forgot to specify what output you'd expect. Or use `print(*isbnl, sep=', ')`. – Martijn Pieters Jul 17 '14 at 14:02

1 Answers1

8

You have two options:

If the items in your list are already strings:

list_of_strings = ['abc', 'def', 'ghi'] # give a list of strings

new_string = "".join(list_of_strings)   # join them into a new string

If the items in your list are not ALL strings, you can must turn them into strings first:

list_of_nonstrings = [1.83, some_object, 4, 'abc'] # given a list of mixed types

# construct new list of strings for join function
list_of_strings = [str(thing) for thing in list_of_nonstrings]

new_string = "".join(list_0f_strings) # join into new string, just like before

Now you may print or manipulate new_string however you need to.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84