Book Title | Sololearn: Learn to code for FREE!

+1

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 https://code.sololearn.com/cTFg60GR2X3I/?ref=app

11/2/2020 7:13:08 PM

Alejandra b

6 Answers

New Answer

+3

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. https://code.sololearn.com/cdaPdMWWeN3W/?ref=app

+1

https://code.sololearn.com/cNFzmrlzqE9u/?ref=app

+1

https://code.sololearn.com/cV44Du4ugn0g/?ref=app Check this

+1

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

0

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

-1

Another simple way to do it 😅 https://code.sololearn.com/cML3C1NNbynn/?ref=app