Book Title | Sololearn: Learn to code for FREE!


Book Title

Hello! I have to do this task, but I don’t know where to start neither how to do it, could you help me please, Thank you! You have been asked to make a special book categorization program, which assigns each book a special code based on its title. The code is equal to the first letter of the book, followed by the number of characters in the title. For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space). 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

11/2/2020 7:13:08 PM

Alejandra b

6 Answers

New Answer


A simpler way with 5 lines of code. A simple explanation, loop through all lines, and if a line has trailing/ending "\n" remove it from the count of the length.


+1 Check this


file = open("/usercode/files/books.txt", "r") for line in file: print(line[0]+str(len(line.strip("\n")))) file.close() You add .strip("\n") after line so it does not count \n as character when you are counting the length


You can see output is a list of values.. Read lines in a loop and print first letter and print length of string...


Another simple way to do it 😅