I am trying to achieve a print where it outputs longest line and the words associated with it.
with open("alice.txt") as file:
for line in file:
words = line.split()
words_count = len(words)
if maxlines == 0 or len(words) > len(maxlines.split()):
maxlines = line
sentences.append(line)
...
maxlines_len = str((len(maxlines.split())))
print("Longest line has " maxlines_len " words: " maxlines)
The variable will spit out a typeError if I declare its value it without str(). Is there any workarounds without fstrings or str()?
Thank you!
CodePudding user response:
Well, you can't sum strings and integers, but print()
happily accepts any number of arguments and will stringify them internally (and separate them with spaces by default; you can control that with the sep=
keyword argument):
print("Longest line has", maxlines_len, "words:", maxlines)
If using f-string formatting was an option (not sure why you wouldn't use them):
print(f"Longest line has {maxlines_len} words: {maxlines}")