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 trial

Python

What does this error mean and how do I fix it?

print (reverse(string))
^
SyntaxError: invalid syntax

5 Answers

You'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

//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)

You 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 the str function is missing :smile:

Add 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.

Just 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. :smile:

Thank you for cleaning up the Community filters :)

Torsten Lundahl
Torsten Lundahl
2,570 Points

There 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.

You are correct, but this specific error isn't caused by that (once you fix the SyntaxError then it will display an error about this)

There is still a syntax error pointing to print, Alexander Davison, Is they're anything else wrong?

//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

//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]

You need to put the [1::-1] inside the print statement next to the string variable.

print(string[1::-1])

I hope you understand now :smile:

~Alex