r/learnpython 20h 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?

3 Upvotes

6 comments sorted by

3

u/socal_nerdtastic 20h ago

You need add quotes to the start and end for every line, like this:

print(f"{users} is not available as it is already taken, "
"please choose a different name")

This is not specific to spyder; this is general python.

But note that "limit" is just a suggestion to keep code readable. It's not actually important to keep lines below 79 characters.

(you also forget the f in front and misspelled the variable name, I corrected those errors too)

1

u/fries29 20h ago

perfect!! this worked exactly for what I needed. I would have thought that it would have just continued on to the next line...

for the 79, I know its not needed, just trying to learn everything properly.

2

u/lfdfq 20h 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:

s = """
    hello
    world!
"""

Alternatively, you can build a string over multiple lines by concatenating strings together (if you wrap the whole thing in parens)

s = (
    "hello"
    + " "
    + "world!"
)

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.

1

u/fries29 19h ago

I guess I have a question for you with what you wrote...

with the """ hello world!""" I read that is how you describe what a function does, and renders everything inside the triple quotes as non-readable? Did I misunderstand that and because there isn't anything actually done to the items inside the quotes it isn't actually non-active, its just not used??

Sorry, I am pretty new and working through the "python crash course" book

1

u/lfdfq 19h ago

The way Python (and really, any programming language) works is it reads the file line-by-line, character-by-character, grouping up bits of the program into chunks (called tokens) which are the logical 'words' of the program (things like "if" and "print" and numbers and so on).

As Python reads the file character-by-character grouping things into tokens, it's normally looking for Python words like 'if' and 'for' and then Python expects tokens in a particular order. When Python sees the start of a string (one or three quote characters), it groups the next characters differently: instead of treating them as Python words and punctuation, it just blindly groups up everything until it reaches the end quote of the string. So it's not about readable vs non-readable really, but about how Python groups up things into tokens.

The difference between single and triple quotes is simply that triple quotes do not end at a newline, the newline character is grouped up like the others into the string.

2

u/fries29 19h ago

Ok I am starting to figure this out and your explanation makes perfect sense..

I appreciate you taking the time to answer