0

Exception and files final project Python Core

https://code.sololearn.com/ccg2x9F1hCBW/?ref=app How can I get the first three numbers correct, and the line separation on point?

15th Oct 2022, 12:11 AM
Meylin Leonario
Meylin Leonario - avatar
2 Answers
+ 1
Use strip() to remove leading spaces at the end of each line, so that you will get your required output print(stringList[line][0], len(stringList[line].strip()), sep="")
15th Oct 2022, 2:20 AM
Niththish
Niththish - avatar
0
Using strip is the cleanest option file = open("/usercode/files/books.txt", "r") #your code goes here vfile = file.readlines() lfile = len(vfile) for i in vfile: flen = len(i.strip()) fini = i[0] print(fini + str(flen)) file.close() Alternatively, you can control when the last element of the list will be printed and add 1 to the lenght: file = open("/usercode/files/books.txt", "r") #your code goes here vfile = file.readlines() lfile = len(vfile) for i in vfile: flen = len(i)-1 fini = i[0] if (vfile.index(i)) != lfile-1: print(fini + str(flen)) else: flen += 1 print(fini + str(flen)) file.close()
9th Nov 2022, 12:15 PM
Luis Miguel Toribio
Luis Miguel Toribio - avatar