Please help cause i dont understand how to complete this | Sololearn: Learn to code for FREE!
0

Please help cause i dont understand how to complete this

Ok so im in the project call "Book titles" from python 3,and i dont know how to erase the ("\n") from the words. The output should be: H12 T16 P19 G18 and mine is: H13 T17 P120 G18 this is my code: file = open("/usercode/files/books.txt", "r") line=file.readlines() for line in line: print(line[0]+str(len(line))) file.close()

21st Jan 2021, 11:41 PM
Melissa Franco
Melissa Franco - avatar
4 Answers
+ 3
Check if line is ending with "\n", If line[-1]=="\n", if yes then use rstrip() method to remove newline character like line=line.rstrip() rstrip() removes from the end of string, and there are strip() and lstrip() as well.
21st Jan 2021, 11:48 PM
Abhay
Abhay - avatar
+ 3
Using rstrip method with "\n" as param: for line in line: line = line.rstrip("\n") print(......)
21st Jan 2021, 11:51 PM
HBhZ_C
HBhZ_C - avatar
+ 2
OOOOHHHH IT WORKS THANK YOU BOTH VERY VERY MUCH
22nd Jan 2021, 12:01 AM
Melissa Franco
Melissa Franco - avatar
+ 1
Ohhh thank u very much both
21st Jan 2021, 11:57 PM
Melissa Franco
Melissa Franco - avatar