+ 1
Solved the problem with a crutch, I don’t understand how to do it right. Need count the number of characters in a string.
#Example: #H12 - Harry Potter # if I use //num = len(line)// I have H12. Why? file = open("/usercode/files/books.txt", "r") massive = (file.readlines()) for line in massive : if len(line) == 18: num = len(line) else: num = len(line) -1 name = (line[0]) result = name + str(num) print(result) file.close()
3 Respuestas
+ 3
Alexandr Grebyonkin ,
to remove the trailing newline sequence we can not code just for this constellation of the file content. see the comments:
file = open("/usercode/files/books.txt", "r")
massive = file.readlines()
for line in massive:
#if len(line) == 18: # this is coded to solve the task exactly for this constellation of file content. don't do this.
#num = len(line)
#else: num = len(line) -1
#name = (line[0])
result = line[0] + str(len(line.strip())) # add .strip() here, this removes leading and trailing whitespaces, also the newline sequences
print(result)
file.close()
+ 1
Lothar your advice helped me, thanks.
It turns out that the transition to another line is also a symbol