1

I'm a Python beginner and in one of our class exercises we were given a solution that I don't really understand.

The question:

Program that will reverse a string with a loop.

Here is the solution:

string = "Hello World"
s = ""
for ch in string:
    s = ch + s
print(s)

I am confused as to why this loop begins at the end of the string. As I understand it the new string should be the same as it will loop from the beginning. Can anyone explain why?

2
  • 5
    s = s + ch builds the same string, s = ch + s builds the reverse as it prepends the next character. Commented Dec 8, 2019 at 13:11
  • 1
    Why not pretend that you are the computer and step through a run? You can also put a print(s) in the body of the loop and see it printed as it grows. You need to develop ways to answer such questions on your own. The Python shell is good for quick experiments -- so experiment rather than post to Stack Overflow. Only post when your experiments fail to answer the question (which is unlikely to be the case here). Commented Dec 8, 2019 at 13:13

5 Answers 5

3

Let's step through this:

for ch in string:
  s = ch + s

in the iterations ch and s will be:

ch = "H"
s = "H"

ch = "e"
s = "eH"


ch = "l"
s = "leH"

.
.
.

You're reversing the string because you keep adding the current character to the existing string.

Sign up to request clarification or add additional context in comments.

Comments

1

You'll understand it better if you print it at every step.

string = "Hello World"
s = ""
for ch in string:
    s = ch + s
    print(s)
H
eH
leH
lleH
olleH
 olleH
W olleH
oW olleH
roW olleH
lroW olleH
dlroW olleH

Basically, you add the new string at the beginning. So in the second step, e will be appended, before H, and so on.

2 Comments

You may want to print ch as well to make it even more clear
Wonderful, thanks for the help. I get it now. And good advice for any range issue in the future.
1

The loop is not starting at the end of the string. I think you are confused by the fact that you are prepending the chars pulled from string to s:

s = ch + s

If you want s to NOT be in reverse order, just reverse the addends there:

s = s + ch

Finally, the Pythonic way of reversing a string is through slicing:

string = 'Hello World!'
print(string[::-1])
# !dlroW olleH

Comments

0

You could do it as below:

s = ''
for i in range(len(string) - 1, -1, -1):
    s = string[i] + s

You could do the other way around as:

s = ''
for character in string:
    s = character + s

Comments

0

for ch in string: s = ch + s

Every letter is stored in the variable ch when you loop through the string, and appended as the prefix to the string s.

Example: 1) Initially, s is an empty string. s=""

2) ch 'H' is added as the prefix in variable s(which is empty initially,ie. "") in first iteration, where ch is 'H' s="H"

3) ch 'e ' is now added as the prefix in variable s( "H"),in second iteration, where ch is 'e' now s="eH"

4) ch 'l ' is now added as the prefix in variable s( "eH"),in second iteration, where ch is 'l' now s="leH"

Similarly, at the end of all the iterations, You will get your reversed string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.