+ 2
Is there any way such that the words inside can be determined without using ,(comma) and separating them all with "(double quote
words = ["spam", "egg", "spam", "sausage"] print("spam" in words) print("egg" in words) print("tomato" in words)
4 Answers
+ 2
Not really. Python lists are defined using commas and brackets. But you can easily turn a space-separated string into a list:
words = "spam egg spam sausage".split()
print(words)
#Output is ['spam', 'egg', 'spam' , 'sausage']
+ 2
partha well... it's actually same as Kishalaya's, only changed space to double quotes
if you mean the double quotes in single ones:
in python, both single and double quotes can used to define a string object
for example,
'with single quote'
"with double quote"
and when you used double quotes to define the string, you don't need a backlash("\") even if there's single quotes in the string, and vice versa
for example,
"It's a string" == 'It\'s a string'
"This: \", is a double quote" == 'This: ", is a double quote'
+ 1
you mean this?
words = 'spam"egg"spam"sausage'.split('"')
if the string used '(single quote), the double quotes inside will be treated as an character, and vice versađ
0
hey Flandre Scarlet sry I didn't get what you said can you give another example or Link to some code like that?