String slicing issue in pycharm


#1

I tried implementing the program in pycharm and I’m getting this print str1.find(str2)

SyntaxError: invalid syntax

str1 = “this is string example…wow!!!”;
str2 = “exam”;

print str1.find(str2)
print str1.find(str2, 10)
print str1.find(str2, 40)


#2

what python version are you using?


#3

If I had to guess, I’d say that you’ve got Python 3.x installed.

In Python 2.x, print worked how you’ve written it. With Python 3, it changed to a function call.

# this is valid in Python 2.x:
print "I print strings in Python 2!"

# this is valid in Python 3.x:
print("I print strings in Python 3!")

If you need to check your Python version, you can open the command line and type python and it’ll tell you on the first line. :slight_smile:


#4

i know, i just wanted to let them figure it out. The less we do, the better it actually is in my opinion.


#5

On the whole, I agree with you that it’s better to nudge the OP towards the answer rather than giving them the solution outright which is what I try to do when I’m replying on here.

That being said, though, I’m pretty sure that the Codecademy Python course only briefly mentions right at the start that it’s based on Python 2 rather than Python 3. That means it’s all too easy for someone, whose only experience with Python is on here, to go to python.org, grab the download for the highest version number they see, and install it and get going. They wouldn’t expect print to behave any differently to how they’ve been taught it on here.

If they didn’t remember the Python 2.x/3.x bit, then worst case scenario you could end up with someone being put off Python (or programming in general) because their confidence was given a kicking by having such a seemingly simple instruction throw endless errors at them. Especially after they’ve been taught to do it specifically the old way.

Seemed better to just point out why print was misbehaving, rather than having them sit tearing their hair out because of something so simple. :slight_smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.