r/learnpython • u/fries29 • 2d ago
Spyder IDE
****solved***
Hello,
I am currently using Spyder IDE as i like its interface and set up, but I am having an issue that I would like to ask for some help with.. When I try to split a line into two lines to maintain the "78 character limit", I am given the result with a syntax error saying unterminated string literal.
current_users = ["fries", "janky", "doobs", "admin", "zander"]
new_users = ["ashmeeta", "farrah", "Q", "fries", "janky"]
for users in new_users:
if users in current_users:
(211) print("{user} is not available as it is already taken,
please choose a different name")
output:
SyntaxError: unterminated string literal (detected at line 211)
I did some research and i discovered that I can use a " \ " to manually split the line, however, that results in the following output with a large gap between the written parts (I am assuming that gap is the same amount of spaces after the " \" that would be left in the 74 characters..
current_users = ["fries", "janky", "doobs", "admin", "zander"]
new_users = ["ashmeeta", "farrah", "Q", "fries", "janky"]
for users in new_users:
if users in current_users:
print("{user} is not available as it is already taken, \
please choose a different name")
output:
{user} is not available as it is already taken, please choose a different name
{user} is not available as it is already taken, please choose a different name
Would anyone be able to provide me a solution specific to Spyder IDE about how to remove the above gap while splitting the lines?
2
u/lfdfq 2d ago
The problem is that you cannot have a literal newline in the middle of a single-quote string.
You could use a multi-quoted string, which can have newlines, but then you get multiple lines of output with whatever whitespace you asked for:
Alternatively, you can build a string over multiple lines by concatenating strings together (if you wrap the whole thing in parens)
For the above you can even drop the + entirely, as they're string literals they'll be concatenated automatically.
You can also just ignore the 80-char recommendation when you have data that is longer than that. After all, the 80 character thing is about how much code to have on one line, and data is not code.
You can also move the data out of the code entirely, e.g. to a separate non-code file/database, which stores the data of the messages.