+ 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()
14 ответов
+ 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()
+ 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()
+ 2
Thank you Lemesa
+ 1
Hi! For better help to you, please, show us your code attempt! Thx!
+ 1
Also
for line in file:
print (line[0]+str(len(line.strip())))
+ 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()
+ 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()
+ 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()
0
Thanks Lemesa
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.
0
Turns out, all I needed was that "\n". :)
That little detail kept me up all night.
Thanks a bunch
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.
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
0
Thank you