+ 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

2nd Nov 2020, 7:13 PM
Alejandra b
Alejandra b - avatar
8 Respostas
+ 5
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
29th Dec 2020, 6:26 PM
Lam Wei Li
Lam Wei Li - avatar
+ 2
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
8th Jun 2021, 7:05 PM
Jacques Savary Benjy Adolphe
Jacques Savary Benjy Adolphe - avatar
2nd Nov 2020, 9:56 PM
Mohsen Abbaspour
Mohsen Abbaspour - avatar
+ 1
https://code.sololearn.com/cV44Du4ugn0g/?ref=app Check this
29th Dec 2020, 9:40 PM
AG20_R
0
You can see output is a list of values.. Read lines in a loop and print first letter and print length of string...
2nd Nov 2020, 8:39 PM
Jayakrishna 🇼🇳
- 1
file = open("/usercode/files/books.txt", "r") f = file.readlines() #your code goes here for v in f: w = v.rstrip() print(w[0][0]+str(len(w))) file.close() That's it â˜ș
11th Jun 2022, 9:49 AM
M.Liaichi
M.Liaichi - avatar
- 1
file = open("/usercode/files/books.txt", "r") for line in file: words = line.split() i = "" for word in words: i += word[0] print(i) file.close()
22nd Dec 2022, 5:48 AM
Juan David Ortiz Guevara
Juan David Ortiz Guevara - avatar
- 2
Another simple way to do it 😅 https://code.sololearn.com/cML3C1NNbynn/?ref=app
8th Jan 2021, 8:07 PM
George Pro
George Pro - avatar