Book title q | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Book title q

Hi, my code and output is shown below file = open("/usercode/files/books.txt", "r") #your code goes here lines = file.readlines() for line in lines: print(line[0]+str(len(line))) file.close() Your Output H13 T17 P20 G18 I think the print() function causes the newline ending problem. I’ve considered using end=“” and it gets rid of the newlines I do want. I do not know what resolvers there are without my code looking a little bit hacky. I’m interested in a clean way to get my code to work

12th Jan 2022, 2:14 AM
Geoffrey
Geoffrey - avatar
5 Answers
12th Jan 2022, 3:48 AM
Oma Falk
Oma Falk - avatar
+ 2
I see. So, the problem wasnt with the newline ending. It was that I was overcounting the length due to there being a newline character at the end of a line
12th Jan 2022, 3:26 AM
Geoffrey
Geoffrey - avatar
13th Jan 2022, 2:12 AM
Saydimurod Qodirjonov
Saydimurod Qodirjonov - avatar
+ 1
You can use repr instead of print to see what the line contains.
12th Jan 2022, 2:21 AM
FanYu
FanYu - avatar