0
def reverse(s): 
  str = "" 
  for i in s: 
    str = i + str
  return str
  
s = "Geeksforgeeks"
  
print ("The original string  is : ",end="") 
print (s) 
  
print ("The reversed string(using loops) is : ",end="") 
print (reverse(s))

I tried the code in my own way to understand how the above method is reversing the string entered in s in my own way

i will post what i tried to know where I have gone wrong in my understanding

s='preetham'
for i in s:
    str=''
    s=i+str
    print(s)

I tried the above code to just understand what role is i and str playing in helping the code to reverse the string, as I predicted from my understanding the above code should print the following output

p
r
e
e
t
h
a
m
*
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    Write the variables down on a piece of paper and then execute the code by hand to see how it works. – Barmar Jan 07 '21 at 06:23
  • Change `str = i + str` to `str = str + i`...What happens then? An additional tip is to add `print(str)` after the `str = ...` line, to see how the string is being created. – BruceWayne Jan 07 '21 at 06:24
  • Your code prints the current letter of the string iteration before an empty string. Doesn't reverse anything or accumulate a larger string – OneCricketeer Jan 07 '21 at 06:31
  • Besides, the conventional python solution is `return s[::-1]` – OneCricketeer Jan 07 '21 at 06:33
  • @BruceWayne heyyy that was helpful but still I am confused on what role is str='' playing in reversing. –  Jan 07 '21 at 06:41
  • @OneCricketeer exactly ''Your code prints the current letter of the string iteration before an empty string. Doesn't reverse anything or accumulate a larger string'' yes that is why I am confused by this answer `return s[::-1]` This is an easy one to understand `return s[::-1]` and I know this works and is easy to understand for me maybe because I know about slicing operations and how negative indexing works. But the first question still confuses me on how it is working. –  Jan 07 '21 at 06:44
  • You've misplaced your `str=''` inside the loop, so it's effectively doing nothing. When outside the loop, it's adding characters and creating a new, larger `str` – OneCricketeer Jan 07 '21 at 06:48

1 Answers1

0

For the output p r e e t h a m you should use :

s='preetham'
str = ''
for i in s:
    str=str + i + ' '
print(str)

So, What this does?

First line we store preetham in a variable s.

Second we initialize a variable str with ''[an empty string] to store the new string

Third we start a for loop.
Here what does the for loop do? The variable i extracts each character[or you may call letter here] of strings one by one and each time executes the code within the loop with each value of i.

Inside the loop:
We append each character[stored in i] of to str along with a space [' '].

Last line outside the loop we print the value of str which is now p r e e t h a m

So What happens when we run the program?

First i = 'p' & str = '' str = '' + 'p' + ' ' [ = 'p ']

Then i = 'r' & str = 'p ' str = 'p ' + 'r' + ' ' [= 'p r ']

...

Finally i = 'm', & str = 'p r e e t h a ' str = 'p r e e t h a ' + 'm' + ' ' [ = 'p r e e t h a m ']

Lastly in print we print str: p r e e t h a m

So.

p
r
e
e
t
h
a
m

Will be printed if just use '\n' instead of ' '

Now You can understand the 2 other codes too by this explanation.

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29