Bill Reason Bill Reason - 4 months ago 23
Python Question

How to split up a string into separate characters in Python

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


#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'

Thanks for any answers.


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:

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

End result: