M
M
Michael S.2021-07-19 14:54:07
Python
Michael S., 2021-07-19 14:54:07

Working with files, why doesn't it output what I expected?

file = open('books.txt', 'r')

for lis in file:
    f = 0
    print(lis[f] + str(len(lis)))

I'm trying to access a file and expect a response from it:
H12
T16
P19
G18

and it gives me:
H13
T17
P20
G18

In books.txt file:
Harry Potter
The Hunger Games
Pride and Prejudice
Gone with the Wind

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-07-19
@BUUSSA

Because there is a line break character at the end of the line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question