Python 88 characters maximum in a line

I use teams for education and some of my students are having issues with typing more than 88 characters in one line of code. When I try it myself on my account it works, but for some reason my students aren’t able to print the following statement:

print(name1 + " is a " + job1 + " and will have a " + pet1 + ". They will drive a " + cars1 + " and marry " + partner1 + " while living in a " + house1 + ".")

We were able to fix it by separating it into 2 print lines, but this seems really silly. Any idea what could be causing this error?

1 Like

A few notes here:

  1. That’s just a warning, the code should still run just fine
  2. Perhaps f-strings would work better there?
print(f"{name1} is a {job1} and will have a {pet1}. They will drive a {cars1} and marry {partner1} while living in a {house1}.")

Though that could be a future lesson in your course.

4 Likes

It is just a warning because it is generally bad practice to have extremely long lines. It can be safely ignored.
To disable the warning in the default python template, open the pyproject.toml file, going to the ignore variable under the “tool.ruff” section, and adding 'E501' to the list.

Generally, long lines like these can be broken down with python features such as formatted string literals and implicitly concatenated strings.

2 Likes

Yes, I do teach f string, but I use concatenation more because these kids are going to be learning Java in the future and I like to be as consistent as possible between the languages.

1 Like

Suggestion: you can break into multiple lines as long as the code is surrounded by any parentheses or grouping
Example:

print(
  name1 + " is a " + job1 + " and will have a " + pet1
  + ". They will drive a " + cars1 + " and marry "
  + partner1 + " while living in a " + house1 + "."
)
2 Likes

They were aware of that:

1 Like