+ 2

How to solve the Book Titles in python??

Tried a lot bud didn't succeeded. Here's the code I tried - file = open("/usercode/files/books.txt", "r") data = file.readlines() amount1 = file.write(data[0][0]) amount2 = file.write(data[1][0]) amount3 = file.write(data[2][0]) amount4 = file.write(data[3][0]) print(data[0][0] + str(amount1)) print(data[1][0] + str(amount2)) print(data[2][0] + str(amount3)) print(data[3][0] + str(amount4)) file.close()

7th May 2021, 1:02 PM
Ritesh Behera
Ritesh Behera - avatar
14 Antworten
+ 22
Here is the answer i guess it's pretty clear file = open("/usercode/files/books.txt", "r") titles = file.readlines() for i in titles: if '\n' in i: print(i[0] + str(len(i) - 1)) else: print(i[0] + str(len(i))) file.close()
7th May 2021, 1:41 PM
Lemi Elias
Lemi Elias - avatar
+ 5
file = open("/usercode/files/books.txt", "r") list = file.readlines() for i in list: i = i.replace("\n","") print (i[:1]+str(len(i))) file.close()
1st Jun 2022, 1:48 PM
</ang/uk>
</ang/uk> - avatar
+ 2
Thank you Lemesa
8th May 2021, 11:29 AM
Ritesh Behera
Ritesh Behera - avatar
+ 1
Hi! For better help to you, please, show us your code attempt! Thx!
7th May 2021, 1:17 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Also for line in file: print (line[0]+str(len(line.strip())))
17th Sep 2022, 6:26 PM
Tejas Deshmukh
Tejas Deshmukh - avatar
+ 1
A more compact solution. file = open("/usercode/files/books.txt", "r") for i in file.readlines(): print(i[0]+str(len(i)-1)) if "\n" in i else print(i[0]+str(len(i))) file.close()
30th Sep 2022, 5:44 AM
chrissx8
+ 1
file = open("/usercode/files/books.txt", "r") for line in file: if '\n' in line: print(line[0]+str(len(line)-1)) else: print(line[0]+str(len(line))) file.close()
21st Oct 2022, 6:39 PM
Hadi Mkammal
Hadi Mkammal - avatar
+ 1
if anyone is looking at this post today in my opinion one of the simplest solutions would be: file = open("/usercode/files/books.txt", "r") #your code goes here for x in file: book = x[0] + str(len(x.strip("\n"))) print(book) file.close()
28th Mar 2023, 1:55 PM
Lovro Vasilj
Lovro Vasilj - avatar
0
Thanks Lemesa
5th Dec 2021, 12:27 PM
midhun jose
midhun jose - avatar
0
Thank you so much! I have been taking notes all this time. I wouldn't have been able to figure it out on my own.
17th Dec 2021, 9:22 AM
Esther McAngus
0
Turns out, all I needed was that "\n". :) That little detail kept me up all night. Thanks a bunch
21st Dec 2021, 4:16 AM
Boateng Godson
Boateng Godson - avatar
0
Thanks Lemesa. I appreciate your advice. Your tips will be very useful for me as a beginner and for someone who wants to learn python. I am also currently studying at the university and writing research on EU law. At https://writix.co.uk/essay-examples/supremacy-of-eu-law I found very useful information about EU law in general and its supremacy.
27th Apr 2022, 4:32 PM
Boston Gray
0
This is my answer for this : for i in range(len(a)): x=len(a[i]) if "\n" in a[i]: x -=1 x=str(x) print(a[i][0] + x) i+=1
22nd Oct 2022, 2:26 PM
Орест Любчик
Орест Любчик - avatar
0
Thank you
19th Oct 2023, 10:55 PM
Gary Stokley
Gary Stokley - avatar