0

Can one character of a string variable be changed?

For instance, if # "o" is in position 1, can I change position 1 to something else? Basically, I'm asking for user to input a word via

word = input("Please enter your favorite word: ")

Then I'm taking that word variable, and I'd like to parse it down letter by letter. Then I'm trying to change each letter to different things using if then or for i in range.

So I'm trying to do something like

word[0] = "a" word[1]="b"

then I have to run the different scenarios down based on the if then/for i.

Maybe

if word[1] = "b" then word[1]="c"

Then I recompile the word with changes. Can that be done, or am I thinking about this wrong?

martineau
  • 119,623
  • 25
  • 170
  • 301
herbacidal
  • 39
  • 6

1 Answers1

0

The string variables are immutable. You cannot change the word after you define them. If you want to change the letters I suggest you convert the word into a list of characters and then perform whatever operation you want to perform.

Please refer to this post about how to convert a string to a character array.

Ezio
  • 2,837
  • 2
  • 29
  • 45