Aren't strings immutable?

Would anyone be able to tell me why the given solution to this problem works? In an earlier lesson we learned that strings are immutable. It seems like the process of iterating through the letters in the given word and then adding every other one to the string every_other would necessarily require us to change the string every_other.

Is it that strings can be added to, but the individual characters in strings can’t be removed or swapped without creating a new string?

2 Likes

It would seem that way, although we are not changing it, but replacing it with a new composed string value

Only when we do it on the right side of an assignment (or in an argument) using concatenation (or other method) to build the desired string expression. Each time we assign it to the left side. If that is the same variable, then we have replaced its value with the new one.

if a and b are string values,

a = a + b

replaces a with the joint string value. The right hand side is not mutating a string, but creating a single string value.

a += b

says the same thing using the compact assignment operator.

2 Likes

Thank you MTF! Your explanation that we’re “replacing” rather than “changing” the string really clicked for me.

2 Likes


@mtf Dear Roy
Would you please tell me which part of the code leads to the output of “none” ?
Thanks in advance!

Your function prints something then exits. Then the commands print again only there is nothing to print but what comes back from print(), which is None. This is why we do not print inside a function. It makes the result useless to the outside (the program).

Rather than print(returned) in the function, use,

return printed

Thanks for the explanation. I realize I don’t know how exactly the commands would be excuted. So I wonder in my wrong code, the for loop commands print(printed)? and then the def functions commands to print(printed) again? That’s why the second time its output is None, am I right? I am a bit confused regarding the indentation and its relations with the commands or in this case print is commanded again by def function?

1 Like

Yes, the second print only printed what the first one returned, which is None.

I see there is a typo/error in my last post; so glad you overlooked it.

Thanks for liking my post and I get “welcome” Badge from it^^
Thank you again for your reply. I always enjoy reading them.
P.S. I don’t always point out typo/error unless I am not sure about the exact intention/meaning of it.

1 Like

I used the following code:

def every_other_letter(word):
  l=len(word)
  w=word[0:l:2]
  return w

Not the most elegant solution, but it worked as designed.

Hello, @codeitcodeitgood, and welcome to the forums.

If you take into account the default values if you leave the starting index and ending index blank, how could you shorten that up a little bit. Not that you need to, but if you’re feeling up to the challenge…

Also, I formatted your code. Go ahead and check out this post as a reference on how to do this.

I tired this and it worked! Thank you…

`def every_other_letter(word):

w=word[::2]
return w`

If you use a variable once you may as well inline the definition
Usually you’d create a variable so that you can refer to it several times

1 Like
def every_other_letter(word):
  return word[::2]
  
1 Like

Don’t forget indentation.

This is what i came up with

def every_other_letter(word):
  a = word.split()
  for i in range(len(a)):
    return word[i::2]

that’s not a loop.

sorry i didn’t get it…will you please explain as it will surely benefit me alot

You’d use a for-statement to repeat something.
The action in your for-statement is not repeated. It is carried out at most once.

If you want to carry something out once, then just write it as it is.

print('write this once')

don’t put it in a loop.

for _ in range(len('x')):
    print('write this once')

so if one wants to repeat something for only one time, we shouldn’t use loop

Statements already execute once by default. You don’t need to put everything in a loop to get it to run. Not sure what you’re asking.

You are using a loop, but the thing you’re doing only happens at most once, there’s no reason to use a loop.

you have this:

function:
    do many times:
        exit the function

you can’t exit multiple times. once you’ve exited you’re no longer executing that code.