How Do I Split My String Into Multiple Variables

Question:

I am reading data from a text file and when I print the content I need to split each word to its own variable.

So far I have:

f = open(file='Input.txt', mode='r')
content = f.read()
split = content.split()
print(split)
f.close()

Output:

['verb', 'verb', 'adj', 'noun', 'name', 'verb', 'adj', 'adj', 'noun', 'name', 'number', 'noun', 'adj', 
'verb']

How would I split this string so that every word is assigned to it’s own variable?

ex:

1 = ‘verb’

2 = ‘verb’

3 = ‘adj’

Asked By: Bluffyyy

||

Answers:

If you know the length, it’s rather easy:

one, two, three = [1, 2, 3]

I’d also look into namedtuple since it seems to loosely fit your requirements. However, I feel like you’d be better served by using a dictionary. I don’t know what your end goal is though.

If you could explain why exactly you want to do this, I could probably provide a better answer, since dynamic assignment of variables is typically not a good practice in any language

Answered By: Jam
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.