Book titles | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Book titles

How do i complete that practice called book titles, and i am stuck at that part to print first char and num of chars including spaces

17th Oct 2020, 11:41 AM
JFMINEBOY João Felipe
JFMINEBOY João Felipe - avatar
16 Answers
+ 4
Post the code you have tried. You have to: - open the file - give it a name - use readlines() to make a list of titles - print the first letter of each title and its length - for all the titles except the last one, shorten the length by 1 so as not to count the "\n" at the end. - close the file
17th Oct 2020, 12:06 PM
David Ashton
David Ashton - avatar
+ 4
A simpler way with 5 lines of code. A simple explanation, loop through all lines, and if a line has trailing/ending "\n" remove it from the count of the length. https://code.sololearn.com/cdaPdMWWeN3W/?ref=app
15th Dec 2020, 6:36 PM
Lam Wei Li
Lam Wei Li - avatar
+ 3
probably by using title(). But the only way anyone can help for sure is by posting your code, with a description of where you're stuck JFMINEBOY João Felipe post your code so we can see where the problem might be
17th Oct 2020, 11:42 AM
Slick
Slick - avatar
17th Oct 2020, 12:39 PM
Oma Falk
Oma Falk - avatar
+ 2
So far so good. Now that you have a list of the titles, loop through it and print the first character and length of each title.
18th Oct 2020, 2:59 AM
David Ashton
David Ashton - avatar
+ 2
I finished the task but I wonder why \n counts as one character instead of two ("\" and "n")
4th Aug 2021, 8:42 PM
Marc Raventós
Marc Raventós - avatar
+ 2
Rami Muleys line[] is cyclic. line[0] is the first character. If you have 10 characters, line[9] is the last character. Due to cyclic, line[-1] is the also last character. And this is more dynamic and it works all for numbers of character length.
7th Sep 2021, 3:53 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
How using readlines? I am trying titles = file.readlines()
17th Oct 2020, 12:22 PM
JFMINEBOY João Felipe
JFMINEBOY João Felipe - avatar
+ 1
But i get invalid syntax on a line that do not exists?!
2nd Nov 2020, 10:07 PM
JFMINEBOY João Felipe
JFMINEBOY João Felipe - avatar
+ 1
JFMINEBOY João Felipe I suspect you may have copied and pasted a nonprinting character. I suggest starting over with a new code and type it in from scratch.
3rd Nov 2020, 4:45 AM
David Ashton
David Ashton - avatar
+ 1
thank you Lam
8th Sep 2021, 1:27 PM
Rami Muleys
0
- open the file - give it a name - use readlines() to make a list of titles - print the first letter of each title and its length - for all the titles except the last one, shorten the length by 1 so as not to count the "\n" at the end. - close the file file = open("/usercode/files/books.txt", "r") #your code goes here lines = file.readlines() for line in lines: letter = line[0] if line[-1] == "\n": number = len(line)-1 else: number = len(line) print (str(letter) + str(number)) file.close()
4th May 2021, 10:24 AM
BRight
BRight - avatar
0
\n here is a new line tag so it is counted as one
8th Aug 2021, 6:51 AM
Aashay
0
ould you please explain why line[-1] is considered as the end of the string?
6th Sep 2021, 9:17 PM
Rami Muleys
0
I am beyond confused how solo learn expects us to know all of this from the stuff they teach us.
18th Jan 2023, 4:31 PM
Oddly Satisfying
Oddly Satisfying - avatar
- 2
#Here is the code file=open("/usercode/files/books.txt", "r") p=file.readlines() for i in p: letter=i[0] if i[-1]=="\n": number=len(i)-1 else: number=len(i) print(str(letter)+str(number)) file.close()
3rd Jul 2022, 4:30 AM
Rishabh malik