+ 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

26th Oct 2022, 5:21 PM
Ben Hamida Mohamed Amine
Ben Hamida Mohamed Amine - avatar
6 Answers
+ 3
Each line having '\n' character at end except the last. You need to not count \n into length.
26th Oct 2022, 6:15 PM
Jayakrishna 🇼🇳
+ 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.
26th Oct 2022, 6:43 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
From the 'line', remove '\n' then find length. (use replace function or strip() ) Or except last line, print length-1
26th Oct 2022, 6:39 PM
Jayakrishna 🇼🇳
+ 1
How Can I do this ?
26th Oct 2022, 6:33 PM
Ben Hamida Mohamed Amine
Ben Hamida Mohamed Amine - avatar
+ 1
Understood, Thanks !
26th Oct 2022, 6:39 PM
Ben Hamida Mohamed Amine
Ben Hamida Mohamed Amine - avatar
+ 1
Thanks , Sir
26th Oct 2022, 6:46 PM
Ben Hamida Mohamed Amine
Ben Hamida Mohamed Amine - avatar