+ 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()
21 Réponses
+ 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
+ 12
George Pro Your code doesn't take into account the "\n" character at the end of all the lines except the last one. 🙂
+ 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()
+ 10
Have a look at the strip() method.
+ 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.
+ 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.
+ 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()
+ 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?
+ 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))
+ 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()
+ 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()
+ 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.
0
hi
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()
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()
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()
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()
- 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!
- 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()
- 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()