-1

In Python would I be able to split a whole word into multiple letter variables, for example:

word = 'because'

Would give:

1 = 'b'    
2 = 'e'    
3 = 'c'    
4 = 'a' 
5 = 'u'
6 = 's'
7 = 'e'
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

2

Dynamic variables are a bad practice and should be avoided. It is very easy to lose track of them, accidentally overshadow them, etc.

Why not use a dictionary instead?

>>> word = 'because'
>>> dct = dict(enumerate(word, 1))
>>> dct
{1: 'b', 2: 'e', 3: 'c', 4: 'a', 5: 'u', 6: 's', 7: 'e'}
>>> dct[1]  # Would be the same as 'var_1'
'b'
>>> dct[5]  # Would be the same as 'var_5'
'u'
>>>

As you can see, it is about the same as dynamic variable names except that the data is stored cleanly in a dictionary object.

0

I'm not entirely sure what you're asking, but you can access the individual characters of a string using indexing:

word = "because"
print(word[0])  # Prints "b"
print(word[1])  # Prints "e"
print(word[2])  # Prints "c"
print(word[3])  # Prints "a"
print(word[4])  # Prints "u"
print(word[5])  # Prints "s"
print(word[6])  # Prints "e"
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121