Skip to content Skip to sidebar Skip to footer

Print Method "invalid Syntax" Reversing A String

Trying to reverse a string in python and can't figure out what is wrong with my program that this is seen as invalid syntax. print(r) ^ SyntaxError: invalid syntax``` Th

Solution 1:

  1. You are missing ) at the end of second line.

  2. You are using empty separator s.split('')

Solution 2:

To reverse the string you can do:

s[::-1]

Solution 3:

I'm not clear how you want this reversing, but I've supplied 3 possibilities

tmp = ''
tmp2 = ' '
s = "Hello! my name is MangoKitty"
r = tmp.join(reversed(s.split()))
t = tmp.join(reversed(s))
u = tmp2.join(reversed(s.split()))
print (
    r, "\n",
    t, "\n",
    u
    )

OUT

MangoKittyisnamemyHello!
yttiKognaM si eman ym !olleH
MangoKitty is name my Hello!

Post a Comment for "Print Method "invalid Syntax" Reversing A String"