Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSimran Bansari
4,517 PointsWhat does this error mean and how do I fix it?
print (reverse(string))
^
SyntaxError: invalid syntax
5 Answers
Alexander Davison
65,469 PointsYou're probably missing a closing parentheses or a symbol in the previous lines of code.
Can you show all your code, since the error is probably happening in above lines of code?
Thank you. ~Alex
Torsten Lundahl
2,570 PointsThere is no reverse function for strings in the python library. If you want to print a string in reverse, you'll be better off slicing the string.
print(string[::-1])
This will print the string in backwards.
If the code is part of a challange it would be nice if you could link the question to it. It makes it easier to elaborate on the problem.
Alexander Davison
65,469 PointsYou are correct, but this specific error isn't caused by that (once you fix the SyntaxError then it will display an error about this)
Simran Bansari
4,517 PointsThere is still a syntax error pointing to print, Alexander Davison, Is they're anything else wrong?
Simran Bansari
4,517 Points//This is the output that I now get when I enter a word//
Put a phrase that you want reversed!! hello
hello
Traceback (most recent call last):
File "reverse.py", line 3, in <module>
print (string)[::-1]
TypeError: 'NoneType' object is not subscriptable
Simran Bansari
4,517 Points//This is my newer code now //
def reverse(str): return str[::-1]
string = str(input("Put a phrase you want reversed!!"))
print (string)[1::-1]
Alexander Davison
65,469 PointsYou need to put the [1::-1] inside the print statement next to the string
variable.
print(string[1::-1])
I hope you understand now
~Alex
Simran Bansari
4,517 PointsSimran Bansari
4,517 Points//This is my code now but I am still getting the syntax error pointing to print??//
def reverse(str): return str[::-1]
string = str(raw_input("Put a phrase you want reversed!!")
print reverse(string)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsYou are missing a ) for the
str
function.It's this line of code:
string = str(raw_input("Put a phrase you want reversed!!")
You need to add another parentheses at the end and you should be good to go. The first parentheses at the end is to close the opening parentheses of the
raw_input
function and the one that is used to close thestr
function is missingAdd that and you should be good to go.
string = str(raw_input("Put a phrase you want reversed!!"))
As you see I added an extra parentheses at the end of the line.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsJust note: If this post helps you, can you please provide either of us (Torsten or I) with a
Best Answer
? It helps a ton with the Treehouse Community filters.Thank you for cleaning up the Community filters :)