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?


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.


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


@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

10 posts were split to a new topic: I used the following code:

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:

    do many times:
        exit the function

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

But in the given answer, we are using For loop too

my_string = "Hello World"
for i in range(len(my_string)):
  print my_string[i]

That repeats. Which you can tell by that there are multiple things printed.
In your code there is no repetition.

If you are doing something many times, use a loop.
If you are doing something once, don’t use a loop.

Okay, thanks @ionatan for explaining. I’m just a beginner so might ask many things many times.

1 Like
# Write your every_other_letter function here:
def every_other_letter(word):
  new_str = ''
  for i in range(len(word) - 1):
    if i% 2 == 0:
      new_str+= word[i]
  return new_str
# Uncomment these function calls to test your function:
# should print Cdcdm
print(every_other_letter("Hello world!"))
# should print Hlowrd
# should print
1 Like

Is there a reason for subtracting 1 from the length?