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

25th Feb 2022, 4:07 PM
Alexandr Grebyonkin
Alexandr Grebyonkin - avatar
3 Antworten
+ 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()
25th Feb 2022, 5:49 PM
Lothar
Lothar - avatar
25th Feb 2022, 4:16 PM
Simba
Simba - avatar
+ 1
Lothar your advice helped me, thanks. It turns out that the transition to another line is also a symbol
26th Feb 2022, 8:03 AM
Alexandr Grebyonkin
Alexandr Grebyonkin - avatar