Python "Book title" project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python "Book title" project

I'm a noob so i just used print("H12\nT16\n..."). And I'm curious how to complete this project properly

13th Oct 2020, 1:20 PM
Aleksei Shevtsov
Aleksei Shevtsov - avatar
60 Answers
+ 115
Answer to this project: 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() 1: "file" refers to opening books.txt file in read mode 2: for line (referring to the book title) in the books.txt file 3: if book title contains "\n" at the end 4: print first letter([0] refers to the first item in the list aka book title) of book title + (total number of characters - "\n") In this case, len is used to calculate the total number of characters in the book titles 5: else = when conditions of if is not met = "\n" is not at the end of the book title 6: print first letter([0] refers to the first item in the list aka book title) of book title + total number of characters (using the function len!!) 7: close books.txt file
26th Oct 2020, 3:35 PM
Leon Tan
Leon Tan - avatar
+ 12
Aleksei Shevtsov , please show us your try. To give you a hint, how you can get rid of the problem with the new-line sequence (\n): - the trailing new-line sequence can be stripped off with the string method strip() or rstrip()
15th Oct 2020, 10:39 AM
Lothar
Lothar - avatar
+ 8
file = open("/usercode/files/books.txt", "r") Book_Titles = file.readlines() for line in Book_Titles: if line != str(Book_Titles[-1]): print(line[0]+str(len(line)-1)) else: print(line[0]+str(len(line))) file.close() ~~~~~~~~~~~~~~~~~~~~OR~~~~~~~~~~~~~~~~~~~~~ file = open("/usercode/files/books.txt", "r") Book_Titles = file.readlines() for line in Book_Titles: if line == str(Book_Titles[-1]): print(line[0]+str(len(line))) else: print(line[0]+str(len(line)-1)) file.close()
4th Jan 2021, 6:58 PM
Arash Ebrahimi
Arash Ebrahimi - avatar
+ 7
X = file.readlines() Y = len(X) For i in range(y): or for i in range(len(x)): A= x[i][0] B= len(A) If i == B-1: Print(A + str(B)) Else: Print(A + str(B-1))
17th Oct 2020, 9:57 AM
Thanh Nguyễn
Thanh Nguyễn - avatar
+ 7
file = open("/usercode/files/books.txt", "r") newList = file.readlines() for line in newList: if line[-1] == "\n": print(line[0]+str(len(line)-1)) else: print(line[0]+str(len(line))) file.close() Just making Use of the readlines method
5th Jan 2021, 9:44 PM
Harish Tadka
Harish Tadka - avatar
+ 6
the answer for the question is, a= file.readlines() for i in a: print(i[0]+str(len(i.strip()))) file.close() explaination: They are asking indirectly that u have to print the 1st letter of the book and the total number of letters in the name of book. In the for loop, i am going the print the first letter of the book name i.e i[0] and I used concatenation. then i am going to print the total numbers of letters, for that at first i am going to find the length of the string without any gaps so i used strip function (i.e) print(i[0]+str(len(i.strip())))
24th Apr 2021, 1:36 PM
S. S U R E N D A R
S. S U R E N D A R - avatar
+ 6
Annie Láng , the reason why the additional space written to the existing file has no effect, is that '\n' will not be appended at the last line, but it creates a new line that contains just '\n'. using the replace() method creates a new string as result, so we have to use: i = i.replace("\n",""), but this is not very efficient. what we can do is to use the rstrip() (right strip) method, that can remove trailing characters. if we do not give any character as argument for this method, all kind of whitespace is stripped. since the newline sequence '\n' is seen as a whitespace, it will be stripped off. file = open("/usercode/files/books.txt", "r") list = file.readlines() for i in list: #i.replace("\n","") print(str(i[0]) + str(len(i.rstrip()))) #[edited] file.close()
5th Jan 2023, 3:49 PM
Lothar
Lothar - avatar
+ 5
Please share your code with us. The best way to do this, it to put it in playground and link it here. Please also give us a description of the task. Thanks!
13th Oct 2020, 1:29 PM
Lothar
Lothar - avatar
+ 5
Danijel Milovanov , since you have asked for possible optimization of the code, i have put your code in the attached file, also some comments and ideas how the code can be improved. https://code.sololearn.com/cYmm2IRV8Azw/?ref=app
25th Aug 2022, 1:18 PM
Lothar
Lothar - avatar
+ 4
Please make your question more clear https://www.sololearn.com/discuss/1316935/?ref=app
13th Oct 2020, 1:28 PM
Steve Sajeev
Steve Sajeev - avatar
+ 3
Its a seen bug in sololearn, in these recent days
13th Oct 2020, 1:36 PM
Steve Sajeev
Steve Sajeev - avatar
+ 3
Biologia , please do not post a new question in an existing question of another person. you have a much better chance of getting a helpful answer by doing a new post. an other reason is: if the original poster will delete his post, everything will be deleted also your question. as your question has been asked frequently, please use the search bar to get answers. BTW: is the file that you are using with pycharm exactly the file used by sololearn? how did you get this file from sololearn? did you re-create if on the windows device? the reason for this behaviour / difference is that normally all lines in the file (except the last one) will be terminated by a "new-line" sequencee, that will also be counted when using len() function.
21st Jun 2021, 5:26 PM
Lothar
Lothar - avatar
+ 2
For line in file: Print(str(len(line)))
17th Oct 2020, 8:40 AM
Thanh Nguyễn
Thanh Nguyễn - avatar
+ 1
It's a very good project The key is to iterate every single line using the for loop next we put an if condition to the for loop where we check if the last character of the line is '\n', if true we print the first letter + length of the line -1 converted to a string. If false print the first letter + length of the line converted to a string file = open("/usercode/files/books.txt", "r") Book_names = file.readlines() for lines in Book_names: if line[-1] == "\n": print (line[0] + str(len(line)-1)) else: print (line[0] + str(len(line)))
14th Apr 2021, 11:23 AM
shubham damania
shubham damania - avatar
+ 1
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
+ 1
the simplest way file = open("/usercode/files/books.txt", "r") for book in file: print(book[0]+str(len(book.strip('\n')))) file.close()
11th Aug 2021, 11:18 AM
kevin ricardo sanchez garcia
kevin ricardo sanchez garcia - avatar
+ 1
file = open("/usercode/files/books.txt", "r") #your code goes here A1=file.readlines() for line in A1: if '\n' in line: print(line[0]+str(len(line)-1)) else: print(line[0]+str(len(line))) file.close()
1st Jan 2022, 4:58 AM
Sakura688
+ 1
A simple answer that can be helpful: file = open("/usercode/files/books.txt", "r") list_books = file.readlines() # Get the file lines in a list for book in list_books: book = book.replace("\n", "") # replace the \n print (str(book[0]) + str(len(book))) file.close()
1st Jan 2022, 2:54 PM
Amani Boughanmi
+ 1
My answer: file = open("/usercode/files/books.txt", "r") #your code goes here for b in file.read().split("\n"): print(b[0].upper() +str(len(b))) file.close() I what i need is to create a list of titles without including "\n" in each item. So i use .split("\n") You know what to do next
15th Jan 2022, 8:54 PM
crossroad999
crossroad999 - avatar
+ 1
file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: count = line.replace("\n", "") print(line[0] + str(len(count))) file.close()
18th Jan 2022, 10:35 AM
Francis Njeri
Francis Njeri - avatar