Reputation: 29
I am creating a script that splits the user input into letters, I have gotten this part down, however, how do I turn these separated letters into individual variables?
message = input("Enter Message) ")
list = list(message)
print(list)
Whilst this does print out the typed string into letters, I want to know how to turn those split letters into their own variables. e.g (h, e, l, l, o)
Is there a way that I can, for example, only print the first letter or the second letter? (so that the letters are split into their own variables)
Upvotes: 0
Views: 42
Reputation: 1018
Just adding some examples:
message = input("Enter Message) ")
message_characters = list(message) # do not use single characters as variable names
for i, char in enumerate(message_characters):
print(f'The {i}{"th" if i>2 or i==0 else "nd" if i==2 else "st"} character is {char}')
# Though note that strings are also iterable:
for i, char in enumerate(message):
print(f'The {i}{"th" if i>2 or i==0 else "nd" if i==2 else "st"} character is {char}')
Upvotes: 0
Reputation: 5611
You can treat the list as a set 'own variables' (accessing them by index).
message = input("Enter Message) ")
l = list(message) # do not use reserved words, as 'list' for variable names
print(l)
print(l[0]) # prints the 1st letter
print(l[1]) # prints the 2nd letter
print(l[-1]) # prints the last letter
print(l[-2]) # prints the letter prior to the last
Upvotes: 2