I am getting multiple errors while trying to add the (half_my_age) variable to the greeting. It works fine without the half my age part. Is there a rule where you can’t have to variables in a line?
greeting= "hello my name is "+name+'' "half my age is"+half_my_age"
File “script.py”, line 4
greeting= "hello my name is “+name+’’ “half my age is”+half_my_age”
SyntaxError: EOL while scanning string literal
No. No such rule. You do have a few syntax errors though. The string literals must begin and end with quotation marks either single or double quotes will do. You must concatenate the variables to the strings using the
+ operator. You must not put quotes before or after the variable name. If you do both, you’ll have a string literal of the variable name instead of a reference to the value the variable is assigned to. You also cannot use the
+ operator between instances of a string and a number, so the number must be converted to a string in order to concatenate it to a string. For example:
some_string = "Hello, World!"
num = 1024
new_string = some_string + " My favorite number is " + str(num) + "."
An EOL ( End of Line ) error indicates that the Python interpreter expected a particular character or set of characters to have occurred in a specific line of code, but that those characters were not found before the end of the line . This results in Python stopping the program execution and throwing a syntax error .
The SyntaxError: EOL while scanning string literal error in python occurs when while scanning a string of a program the python hit the end of the line due to the following reasons:
- Missing quotes
- Strings spanning multiple lines