+ 3

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()

11th Nov 2020, 4:04 AM
Kalliath Abdul Rasheed Shamil
Kalliath Abdul Rasheed Shamil - avatar
21 Respostas
+ 15
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. https://code.sololearn.com/cdaPdMWWeN3W/?ref=app
15th Dec 2020, 6:38 PM
Lam Wei Li
Lam Wei Li - avatar
+ 12
George Pro Your code doesn't take into account the "\n" character at the end of all the lines except the last one. 🙂
9th Jan 2021, 2:50 PM
David Ashton
David Ashton - avatar
+ 11
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()
14th May 2021, 4:31 AM
Елена Леващева
Елена Леващева - avatar
+ 10
Have a look at the strip() method.
11th Nov 2020, 4:29 AM
_cm_
+ 7
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.
11th Nov 2020, 4:17 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 7
You have to remember each line except the last has an extra character ("\n") at the end that isn't part of the title.
11th Nov 2020, 4:24 AM
David Ashton
David Ashton - avatar
+ 3
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()
16th May 2021, 11:28 PM
Linas Tomas Matiukas
Linas Tomas Matiukas - avatar
+ 2
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?
11th Nov 2020, 4:29 AM
Kalliath Abdul Rasheed Shamil
Kalliath Abdul Rasheed Shamil - avatar
+ 2
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= file.read() for line in x: for ch in line : y= ch[0] print (y + str(len(ch))
11th Nov 2020, 3:35 PM
Azad m. A.
Azad m. A. - avatar
+ 2
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()
27th Aug 2021, 10:52 AM
Ahror Abdullayev
Ahror Abdullayev - avatar
+ 1
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()
8th Jan 2021, 3:59 PM
George Pro
George Pro - avatar
+ 1
Linas Tomas Matiukas thanks a lot, i have just solved the issue in your way. It has given me better understanding of the loops.
17th May 2021, 8:04 AM
Елена Леващева
Елена Леващева - avatar
0
hi
12th Nov 2020, 10:07 PM
John
0
This works. file = open("/usercode/files/books.txt", "r") title=file.readlines() #your code goes here A=0 for i in title: if A+1<len(title): print(title[A][0]+str(len(title[A])-1)) elif A+1==len(title): print(title[A][0]+str(len(title[A]))) A+=1 file.close()
5th Jan 2022, 4:22 AM
Kumar Chandrasekaran
Kumar Chandrasekaran - avatar
0
I found a simple solution, which only requires code already taught at that point of the course: file = open("/usercode/files/books.txt", "r") #your code goes here categorylist = file.readlines() for book in categorylist: book = book.replace("\n","") print(book[0]+ str(len(book))) file.close()
8th Jan 2022, 2:45 PM
Garrit
0
This is how I solved it: file = open("/usercode/files/books.txt", "r") #your code goes here lines = file.readlines() for line in lines: print(line[0]+str(len(line.strip("\n")))) file.close() Explanation: 1. I convert the lines from the .txt file to a list with readlines() function 2. I make a for loop that iterates through each line in the new list and prints the folllowing: the first index of the line plus the length of the line, stripped from any "\n", converted to a string functions used in the for loop: print() str() len() strip()
16th May 2022, 1:06 PM
Victor
0
file = open("/usercode/files/books.txt", "r") #your code goes here for item in file.readlines(): a = "".join(item.split("\n")) print (a[0]+str(len(a))) file.close()
26th Jun 2022, 2:42 AM
HE Miaomiao
- 1
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!
14th Oct 2021, 4:55 AM
Ayeishia Neal
- 1
Hello solo community, heres my solution: file = open("/usercode/files/books.txt", "r") # I believe that my solution will run in O(N)-time #counter for EVERY character in each line count = 0 for line in file: #iterating though ever character for char in range(len(line)): # counting all char that arent '\n' if line[char] != '\n': count += 1 #incrementation # output print(line[0] + str(count)) # rest count count=0 file.close()
15th Jul 2022, 7:55 AM
Aiden Peace
Aiden Peace - avatar
- 2
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()
12th Jul 2021, 7:26 PM
Attila
Attila - avatar