-4

Would it be possible to take a string and set a different variable for every character of the string? In other words..

string='Hello'

#Do some thing to split up the string here

letter_1= #The first character of the variable 'string'
letter_2= #The second character of the variable 'string'
#...
letter_5= #The fifth character of the variable 'string'
Georgy
  • 12,464
  • 7
  • 65
  • 73
Bill Reason
  • 5
  • 1
  • 3
  • 2
    Why would you want to do that? You can already access each character in a similar manner. `letter_1` would be `string[0]`, `letter_2` would be `string[1]`, and so on. – TigerhawkT3 Nov 18 '16 at 17:10
  • 1
    See this thread http://stackoverflow.com/questions/4978787/how-to-split-a-string-into-array-of-characters-with-python – Rode093 Nov 18 '16 at 17:13
  • Does this answer your question? [How to split a string into characters and assign each character to a separate variable](https://stackoverflow.com/questions/47501324/how-to-split-a-string-into-characters-and-assign-each-character-to-a-separate-va) – Georgy Jun 21 '20 at 13:28

1 Answers1

-2

In Python, strings are immutable, so you can't change their characters in-place. However if you try to access by index then you get:

TypeError: 'str' object does not support item assignment

In order to change a character of the string,firstly convert the string into a list of characters,make your desired modification and then make a new variable to store the the new string made by using .join() Take this for example:

string='Hello' 
print(string)
s = list(string)
s[0] = "M"
new_string = ''.join(s)
print(new_string)

End result:

Hello
Mello
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
  • I don't know where you got the idea that this was desired. It may have been accepted (for some reason), but future visitors actually looking for an answer to that question will see an unrelated task in this answer, which doesn't really help. – TigerhawkT3 Nov 18 '16 at 17:41
  • "Would it be possible to take a string and set a different variable for every character of the string? " Then OP shows the code to which I responded with the first part of my answer.Then on the title OP asks "How to split up a string into separate characters in Python" to which i gave a solution. – Taufiq Rahman Nov 18 '16 at 17:46
  • 1
    They obviously meant that they want to set a variable representing each of the string's characters, as they show in their code block, rather than modifying the string with a new value for each character. The OP is new to the site; they might even think they're required to accept an answer. As it is, this looks more like an answer to a different question. – TigerhawkT3 Nov 18 '16 at 17:47
  • I down voted because this does not answer the **main** question. The OP is asking how to dynamically create variables for each character in his string. Not how to modify a string. – Christian Dean Nov 18 '16 at 17:50
  • It's not clear what we wanted.But it did solve his problem so maybe he did not use the right words to ask the question. – Taufiq Rahman Nov 18 '16 at 17:56