Python core Lesson 51 | Sololearn: Learn to code for FREE!

+3

Python core Lesson 51

It works but I don't know why i should ad "-1" for all lines but not for last line file = open("/usercode/files/books.txt", "r") for f in file: if not (len(f) ==18): print (f[0] + str((len)(f)-1)) else: print (f[0] + str((len)(f))) file.close()

9/13/2021 8:31:42 AM

Матвей Стаселько

16 Answers

New Answer

+9

Матвей Стаселько , the code you are presenting is "hard-coded" as it only checks if length is a value of 18 what you can do: ▪︎in your code, the variable 'f' contains the respective string from the file in each iteration cycle of the for loop. all lines (exept the last ones) coming from the file are terminated with a newline- sequence. this can be removed from the string by using strip() like f.strip(). if there is no new-line sequence nothing happens. ▪︎strip removes all kind of whitespace. this can be a simple space, as well as a new-line sequence. happy coding and good success!

+6

Матвей Стаселько , taking the code from Alex and do some changes: file = open("/usercode/files/books.txt", "r") for line in file: print(line[0] + str(len(line.strip()))) file.close()

+4

Матвей Стаселько , practicing is important to grow the coding skills. try to solve simple tasks in simple and clear code. after some time you may go back to your code and try to improve it. always keep in mind that a good readability is the key point for maintaining code. an other way to improve your ability is reading codes from other people, like in playground, or in the q&a section. take codes that people have posted and also raised a question to it. try to find a solution first without having a look what people have posted. then later on have a look at their solutions not to forget websites like geeksforgeeks, tutorialspoint and others. also see realpython, which have a lot of free tutorials. to get dedicated answers, stackoverflow is the one of the best sources. as reading in the web can sometimes be tiring, also have a look at videos from YouTube. try to find what suits you best happy coding and good success!

+2

Each line has a newline character appended to it except last line(obviously)

+2

Thank you! Now I get it!

+1

Abhay, How can I add last line for "else" if I don't know the lenght of this line for example?

+1

Матвей Стаселько sorry buy i don't understand what you mean by not knowing the length of last line . Anyway ,Why do you need to know the length of last line ?

+1

Lothar, Which line I should add strip()?

+1

Add strip to every line . rstrip() will remove white spaces(including newline) from the end of line .

+1

Abhay, When I do like this There are no any changes Last line still less than needed:( file = open("/usercode/files/books.txt", "r") for f in file: print (f[0].strip() + str(len(f))) file.close()

+1

Матвей Стаселько you don't need to strip from the f[0] unless question says so. Strip from the one that you are getting length of. and honestly i won't even use strip since you don't know if the white space was deliberately put there or not. Just use rstrip.

+1

file = open("/usercode/files/books.txt", "r") for i in file: new_list = i.strip() print(new_list[0] + str(len(new_list))) file.close()

+1

Lothar, Yes, i have already done like this Thanks for helping!

+1

Lothar I started learning programming some days ago But I understand that SoloLearn's lessons isn't enough to do a lot of things Can You give an advice what i should to read to become more skillful? Sorry for disturbing you

0

I actually don't know what is happening help me out

0

file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: print(line[0]+ str(len(line.strip("\n")))) file.close()