Python: efficient syntax for creating a list of strings

I'm new to python and essentially trying to figure out the syntax that replicates this functionality: strings = ["foo", "bar", "apple"]with something similar to it strings = [foo, bar, apple], so I don't need to put quotes around all the entries. Thank!

+5
source share
6 answers
strings = "foo bar apple".split()
+6
source
strings = r"foo, bar, apple".split(", ")
+2
source

, . strings = list(open("datafile", "r")).

+1

, , , .

, , , , , , , IDE . , , , - , , ; - , .

+1

, :

"" '' , , [1,2,3] . , python. python python, . python, .

- :

foo = "foo"
bar = "bar"
apple = "apple"
strings = [foo,bar,apple]
+1

, ( !), - :

li = []

ch = ("You have entered an empty string ''\n"
      'Type ENTER alone if you want to stop.\n'
      'Type anything if you want to record the empty string : ')

while True:
    e = raw_input('enter : ')
    if e=='':
        x = raw_input(ch)
        if x=='':  break
    li.append(e)

print
print li

:

enter : 123
enter : ocean
enter : flower
enter : 
You have entered an empty string ''
Type ENTER alone if you want to stop.
Type anything if you want to record the empty string : k
enter : once upon a time
enter : 14 * 4
enter : 
You have entered an empty string ''
Type ENTER alone if you want to stop.
Type anything if you want to record the empty string : 

['123', 'ocean', 'flower', '', 'once upon a time', '14 * 4']

, :

['123', 'ocean', 'flower', '', 'once upon a time', '14 * 4']
0

All Articles