Python 3 => 4th project (Book titles) [[solved]] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Python 3 => 4th project (Book titles) [[solved]]

Have doubt why "\n" was included in character count? {Counted in len()} My Code: file = open("/usercode/files/books.txt", "r") for line in file : print(line[0]+str(len(line))) file.close() My Output: Expected Output: H13 H12 T17 T16 P20 P19 G18 G18

12th Oct 2020, 6:19 PM
Pranav Hirani
Pranav Hirani - avatar
22 Answers
+ 26
file = open("/usercode/files/books.txt", "r") for line in file : if line[-1] == "\n" : print(line[0]+str(len(line)-1)) else : print(line[0]+str(len(line))) file.close() Now it's fine and running
12th Oct 2020, 6:38 PM
Pranav Hirani
Pranav Hirani - avatar
+ 9
Here is a one liner to evaluate the solution: file = open("/usercode/files/books.txt", "r") #for loop for line in file: #one line print statement print(line[0] + str(len(line.strip()))) file.close()
12th Jan 2021, 3:48 AM
Jeramy
Jeramy - avatar
+ 6
Thanks codemonkey ☺️
12th Oct 2020, 6:38 PM
Pranav Hirani
Pranav Hirani - avatar
+ 6
A bit late, but i think it's worth to have a view on it. i will introduce a code that reads a txt file, and strips off the "\n" new line sequence. the code uses a generator object, that means that it can read any size of input file without wasting memory and time. after this, the "\n" sequence will stripped off without hassle https://code.sololearn.com/cYQO6wGf8888/?ref=app
12th Oct 2020, 7:20 PM
Lothar
Lothar - avatar
+ 5
Hirani Pranav , strip() is a string method that removes all preceding and trailing "whitespaces" in a string. whitespace is not only the space you are using to separate words, but also tabs "\t" and newline sequences "\n". so in case of this book project, it makes us able to remove all the newline sequences from the end of a string when reading it from a file.
21st Jan 2021, 10:52 AM
Lothar
Lothar - avatar
+ 4
file = open("/usercode/files/books.txt", "r") books = file.readlines() for book in books: initial = book[0] book = book.replace('\n', '') char = len(book) print(initial+str(char)) file.close()
19th Mar 2022, 6:25 AM
Joel Perez
Joel Perez - avatar
+ 3
file = open("/usercode/files/books.txt", "r") for line in file : print(line[0]+str(len(line.strip()))) file.close()
14th Apr 2021, 3:26 AM
Abdullah Alsalem
Abdullah Alsalem - avatar
+ 3
Yusril , what you are doing is not reading from the file, but just printing the expected result. this not what the exercise is made for.
17th May 2022, 2:22 PM
Lothar
Lothar - avatar
+ 2
Lothar apparitiable 👏
12th Oct 2020, 7:27 PM
Pranav Hirani
Pranav Hirani - avatar
+ 2
Thanks Lothar You said it before but I can't understand at that time. But at this time it's all clear ☺️
21st Jan 2021, 10:58 AM
Pranav Hirani
Pranav Hirani - avatar
+ 2
#your code goes here list = file.readlines() k = len(list)+1 newlist = [] i = 0 while i < k-1: newlist.append(list[i]) i += 1 finallist = [] for element in newlist: finallist.append(element.strip()) for line in finallist: print(line[0]+str(len(line)))
4th Mar 2021, 11:46 PM
Ion Burlacu
Ion Burlacu - avatar
+ 1
file = open("/usercode/files/books.txt", "a") file.write("\n") file.close() file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: print(line[0]+str(len(line)-1)) file.close()
13th Jan 2022, 7:45 AM
Adrian Vreme
Adrian Vreme - avatar
+ 1
file = open("/usercode/files/books.txt", "r") list = file.readlines() for i in list: if i[-1] == "\n": print(i[0] + str(len(i)-1)) else: print(i[0] + str(len(i))) file.close()
27th Mar 2022, 11:31 PM
Thiago Ozelami
Thiago Ozelami - avatar
0
Glad I found this solution. I had the same issue with that newline.
16th Oct 2020, 5:50 AM
Chad_Champion_69🇧🇩
Chad_Champion_69🇧🇩 - avatar
0
Thanks Jeramy
17th Jan 2021, 7:43 PM
Pranav Hirani
Pranav Hirani - avatar
0
file = open("/usercode/files/books.txt", "r") for line in file: print(line[0], end=' ') print(len(line) - 1) file.close()
30th Jan 2021, 11:23 AM
Okan
Okan - avatar
0
file = open("/usercode/files/books.txt", "r") #your code goes here books=file.readlines() for i in books: if i==books[-1]: value=len(i) else: value=len(i)-1 print(i[0]+str(value)) file.close()
25th Apr 2021, 10:28 AM
A Owadud Bhuiyan
A Owadud Bhuiyan - avatar
0
f = open("/usercode/files/books.txt", "r") b = f.read() x=b.split("\n") for i in x: l=len(i) c=i[0] print(c+str(l)) f.close()
17th Jul 2021, 8:44 AM
Smriti Sawhney
Smriti Sawhney - avatar
0
I used replace to remove "\n" and replace it by "" file = open("/usercode/files/books.txt", "r") for line in file: line1=line.replace("\n","") print (line[0]+str(len(line1))) file.close()
15th Sep 2021, 7:03 PM
Harti Dhayaa Eddine
Harti Dhayaa Eddine - avatar
0
file = open("/usercode/files/books.txt", "r") booklist = file.readlines () for book in booklist : book = book.strip () print (book[0]+str(len(book))) file.close()
12th Aug 2022, 2:02 PM
Münciye Okumuş