Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to solve this Python exercise but I have no idea of how to do it: get first character of a line from a file + length of the line

I am learning Python on an app called SoloLearn, got to solve this exercise and I cannot see the solution or see the comments, I don’t need to solve it to continue but I’d like to know how to do it.


Book Titles: You have been asked to make a special book categorization program, which assigns each book a special code based on its title.
The code is equal to the first letter of the book, followed by the number of characters in the title.
For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space).

You are provided a books.txt file, which includes the book titles, each one written on a separate line.
Read the title one by one and output the code for each book on a separate line.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example, if the books.txt file contains:
Some book
Another book

Your program should output:
S9
A12

Recall the readlines() method, which returns a list containing the lines of the file.
Also, remember that all lines, except the last one, contain a \n at the end, which should not be included in the character count.


I tried:

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

for line in file:
    
    for i in range(len(file.readlines())):
        title = line[0]+str(len(line)-1)
        print(titulo)
        
    title = line[0]+str(len(line)-1)
    print(title)
    
file.close

I also tried with range() and readlines() but I don’t know how to solve it

>Solution :

This uses readlines():

with open('books.txt') as f:                # Open file
    for line in f.readlines():              # Iterate through lines
        if line[-1] == '\n':                # Check if there is '\n' at end of line
            line = line[:-1]                # If there is, ignore it
        print(line[0], len(line), sep='')   # Output first character and length

But I think splitlines() is easier, as it doesn’t have the trailing '\n':

with open('books.txt') as f:                # Open file
    for line in f.read().splitlines():      # Iterate through lines
                                            # No need to check for trailing '\n'
        print(line[0], len(line), sep='')   # Output first character and length
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading