Replace a Character (Solution)
00:00
Okay, here’s the new REPL session with the task: Replace "s"
with "x"
in a string. I can kind of call it the sentence is, "Somebody said something to Samantha."
00:13
Python has a handy string method for replacing characters in a string that’s nicely, descriptively named. So I can say sentence.replace()
,
00:25
and then instead of just one argument here, I’ve got to pass two arguments. The first one is the string to replace, so that’ll be "s"
. And the second one is the string to replace the first one with, And that should be "x"
.
00:39 So this is going to give us the solution and the desired output, but think about what the sentence is going to look like after you run this.
00:49
There you go. So it says "Somebody xaid xomething to Samantha."
So it did not replace "Somebody"
the `”S” at the beginning, or the "S"
at the beginning of the name "Samantha"
.
00:58
And that’s, again, because these are uppercase characters, and the uppercase "S"
is different than the lowercase "s"
. So just another little exercise to keep in mind that uppercase and lowercase are two different characters in programming.
01:12
Okay. But the solution is here. We have "Somebody xaid xomething to Samantha."
I wonder what it was.
Become a Member to join the conversation.