Python String Syntax Error
Contents |
here for a quick overview of the site Help Center python print syntax error Detailed answers to any questions you might have Meta Discuss
Python 3 Print Syntax
the workings and policies of this site About Us Learn more about Stack Overflow
Python Print Invalid Syntax Error
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Python Invalid Syntax ( String Line 1)
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up SyntaxError: invalid syntax (
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About print *3 output in python Us Learn more about Stack Overflow the company Business Learn more about hiring python 2.7 print syntax error developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the python 3 print format Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Command-line input causes SyntaxError up vote http://stackoverflow.com/questions/22443725/syntaxerror-invalid-syntax-string 3 down vote favorite 1 I have a simple Python question that I'm having brain freeze on. This code snippet works. But when I substitue "258 494-3929" with phoneNumber, I get the following error below: # Compare phone number phone_pattern = '^\d{3} ?\d{3}-\d{4}$' # phoneNumber = str(input("Please enter a phone number: ")) if re.search(phone_pattern, "258 494-3929"): print "Pattern matches" else: print "Pattern doesn't match!" ####################################################### Pattern http://stackoverflow.com/questions/2589309/command-line-input-causes-syntaxerror does not match Please enter a phone number: 258 494-3929 Traceback (most recent call last): File "pattern_match.py", line 16, in
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known https://docs.python.org/2.7/tutorial/errors.html as parsing errors, are perhaps the most common kind of complaint you get http://inventwithpython.com/blog/2012/07/09/16-common-python-runtime-errors/ while you are still learning Python: >>> while True print 'Hello world' File "
learning the language. Here's a list of common errors that result in runtime error messages which will crash your program. 1) Forgetting to put a : at the end of an if, elif, else, for, while, class, or def statement. (Causes “SyntaxError: invalid syntax”) This error happens with code like this: if spam == 42 print('Hello!') 2) Using = instead of ==. (Causes “SyntaxError: invalid syntax”) The = is the assignment operator while == is the "is equal to" comparison operator. This error happens with code like this: if spam = 42: print('Hello!') 3) Using the wrong amount of indentation. (Causes “IndentationError: unexpected indent” and “IndentationError: unindent does not match any outer indentation level” and “IndentationError: expected an indented block”) Remember that the indentation only increases after a statement ending with a : colon, and afterwards must return to the previous indentation. This error happens with code like this: print('Hello!') print('Howdy!') ...and this: if spam == 42: print('Hello!') print('Howdy!') ...and this: if spam == 42: print('Hello!') 4) Forgetting the len() call in a for loop statement. (Causes “TypeError: 'list' object cannot be interpreted as an integer”) Commonly you want to iterate over the indexes of items in a list or string, which requires calling the range() function. Just remember to pass the return value of len(someList), instead of passing just someList. This error happens with code like this: spam = ['cat', 'dog', 'mouse'] for i in range(spam): print(spam[i]) (Update: As a few have pointed out, what you might need is just for i in spam: rather than the above code. But the above is for the very legitimate case where you need the index in the body of the loop, rather than just the value itself.) 5) Trying to modify a string value. (Causes “TypeError: 'str' object does not support item assignment”) Strings are an immutable data type. This error happens with code like this: spam = 'I have a pet cat.' spam[13] = 'r' print(spam) What you probably want is this: spam = 'I have a pet cat.' spam = spam[:13] + 'r' + spam[14:] print(spam) 6) Trying to concatenate a non-string value to a string value. (Causes “TypeError: Can't convert 'int' object to str implicitly”) This error happens with code like this: numEggs = 12 print('I have ' + numEggs + ' eggs.') What you want to do is this: numEggs = 12 print('I have ' + str(numEggs) + ' eggs.') ...or this: numEggs = 12 print('I have %s eggs.' % (numEggs)) 7) Forgetting a quote to begin or end a string value. (Causes “S