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?
2 Respuestas
+ 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="")
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()