Book Titles | Sololearn: Learn to code for FREE!


Book Titles

I am stuck in a code for a project in Python. The problem is to print the first letter and the length of the characters of that particular line. But, I should not print the newline character which appears in the end of each line. The below is my code: file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: print(line[0]+str(len(line))) file.close()

11/11/2020 4:04:20 AM

Kalliath Abdul Rasheed Shamil

16 Answers

New Answer


George Pro Your code doesn't take into account the "\n" character at the end of all the lines except the last one. 🙂


Have a look at the strip() method.


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.


Thanks everyone, with your advices i have also solved the issue in my way. Here my code: file = open("/usercode/files/books.txt", "r") #введите код сюда book_list = file.readlines() for name in book_list: length = len(name.strip("\n")) letter = name[0] print(f"{letter}{length}") file.close()


For instance if there are name of 3 books, 'file' will look like this: FirstBook\nSecond\nThirdOne. So you need to split the 'file' into a list! Now I wish you can solve it yourself.


You have to remember each line except the last has an extra character ("\n") at the end that isn't part of the title.


This is what I ended up doing - first day studying this, so had to rely on what made sense, rather than the previous course slides. Apparently, strings are immutable, but for each list item, I create a new string - after having replaced the \n with nothing. Once that's done, still in the for loop, I'm getting the first character of the string, and the length of that string. file = open("/usercode/files/books.txt", "r") lines = file.readlines() i = 0 while i < len(lines): newstr = lines[i].replace("\n","") # print(newstr) print(str(newstr)[0]+ str(len(newstr))) i+=1 #your code goes here file.close()


Thank you for the swift response. But there is an issue. In the file, there are four lines. But the expected output has three lines. How can I solve this?


Creating another loop counts line numbers, adding a condition to use the other print function (i.e. to stop adding newline) ... anyway, this way it works:- x= for line in x: for ch in line : y= ch[0] print (y + str(len(ch))


the code below work perfectly file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: def first_letter (): return line[0] print(first_letter()+ str(len(line))) file.close()


Linas Tomas Matiukas thanks a lot, i have just solved the issue in your way. It has given me better understanding of the loops.


I solved this problem with string.replace() function file = open("/usercode/files/books.txt", "r") list = file.readlines() for l in list: l=l.replace('\n','') print(f"{l[0]}{len(l)}") file.close()




Piggy backing off what Azad wrote and using .strip, I came across this: books = file.readlines() for i in books: a = i[0] print(a + str(len(i.strip("\n")))) Hope this helps the ones looking for help!


Here is my solution: file = open("/usercode/files/books.txt", "r") for i in file: print(i[0]+str(len(i.strip("\n")))) file.close()


guys, I didnt know how to strip the /n, so I came up with this solition: booktitles = file.readlines() title0 = booktitles[0] code0 = len(title0) - 1 fl0 = title0[0] print(fl0 + str(code0)) title1 = booktitles[1] code1 = len(title1) - 1 fl1 = title1[0] print(fl1 + str(code1)) title2 = booktitles[2] code2 = len(title2) - 1 fl2 = title2[0] print(fl2 + str(code2)) title3 = booktitles[3] code3 = len(title3) fl3 = title3[0] print(fl3 + str(code3)) file.close() i propose it wins thr prize for the iglirst solution for this task :)