I am new to python and essentially trying to figure out the syntax that replicates this functionality: strings = ["foo", "bar", "apple"]
with something similar to strings = [foo, bar, apple]
so that I don't have to put quotes around all the entries. Thanks!
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.).
strings = "foo bar apple".split()
strings = r"foo, bar, apple".split(", ")
You could place all the strings in a text file, one string on each line. Then strings = list(open("datafile", "r"))
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With