+ 1
Help for this code please
'''You are provided a books.txt file, which includes the book titles, each one written on a separate line. Read the title one by one and output the code for each book on a separate line. For example, if the books.txt file contains: Some book Another book Your program should output: S9 A12''' #My code : file = open("/usercode/files/books.txt", "r") for line in file : z = str(len(line)) t = line[0] print (t+z) #My output : #H13 P17 T20 G18 #expected output : '''H12 P16 T19 G18
6 Answers
+ 3
Each line having '\n' character at end except the last. You need to not count \n into length.
+ 3
Hi, Ben Hamida Mohamed Amine !
One tip is to study the string method strip ( str.strip() ) and then use it to solve your problem.
+ 2
From the 'line', remove '\n' then find length. (use replace function or strip() )
Or except last line, print length-1
+ 1
How Can I do this ?
+ 1
Understood, Thanks !
+ 1
Thanks , Sir