How to solve thid | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to solve thid

You are given a file named "books.txt" with book titles, each on a separate line. To encode the book titles you need to take the first letters of each word in the title and combine them. For example, for the book title "Game of Thrones" the encoded version should be "GoT". Complete the program to read the book title from the file and output the encoded versions, each on a new line.

12th Jan 2023, 10:49 AM
Imran Shakil
Imran Shakil - avatar
19 Answers
+ 6
file is works like a pointer, having total file content. But split is string method. You can use readlines() like file = file.readlines() # to make a list of lines. Then you can apply split in loop. Again you need to separate lines and line words.. edit: may complicated before, reading about the methods you used.. .. file = open("/usercode/files/books.txt", "r") for words in file: for i in words.split(): print(i[0],end="") print() file.close()
12th Jan 2023, 12:03 PM
Jayakrishna 🇮🇳
+ 7
Osmany Baluta , sure, people can say hello, but should not be in the *q&a* section. > it can be done in the feed in the community section.
16th Jan 2023, 4:02 PM
Lothar
Lothar - avatar
+ 6
Imran Shakil , here are 2 sources from sololearn to get more info about string method split(): > tutorial *python for beginners*, *functions*, lesson 39.1 / *string functions* > https://www.sololearn.com/learn/Python/2456/?ref=app
12th Jan 2023, 4:04 PM
Lothar
Lothar - avatar
+ 6
"thid" is a typo of "this" Here, the original poster wants to know how to solve the task from one of the courses.
16th Jan 2023, 4:35 PM
Ausgrindtube
Ausgrindtube - avatar
+ 5
Osmany Baluta , not clear what your issue or question is. please update your post with: > a clear and complete task description > a description that points to your issue, including a possible error messages > link your code try here if this is nof your intention, please use the personal feed in the *community* section.
14th Jan 2023, 12:21 PM
Lothar
Lothar - avatar
+ 5
Arshadvlog , this is not the place to say hello. > *q&a* section is meant for raising coding related questions and discussions.
14th Jan 2023, 12:23 PM
Lothar
Lothar - avatar
+ 5
Navaneethan R , Haris , it would be better if you start your own question, because the current discussion was started by someon else. hence people may not be aware of your issue. > this is also not the place for general comments. > *q&a* section is meant for raising coding related questions and discussions.
14th Jan 2023, 12:29 PM
Lothar
Lothar - avatar
+ 5
أحمد عبد الرازق Jacinto Tejera González , it is better for you to start your own post, otherwise poeple will not get aware of you.
14th Jan 2023, 12:30 PM
Lothar
Lothar - avatar
+ 3
Imran Shakil May be you can find it in string lesson, string methods. But i think, may or may not detailly, not sure. Other way : hope this may help... https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_string_split.asp
12th Jan 2023, 12:57 PM
Jayakrishna 🇮🇳
+ 2
Open file. Read each line. and split into words. Print first letter of each word. That's it. here what is your difficulty..? Lesson have example for each step. Revise once before attempting...
12th Jan 2023, 10:55 AM
Jayakrishna 🇮🇳
+ 2
In other way: by using loop, going through each character of string, print the letter after the space letter. But with split : if str1 is the string you read, then you can split into list of words as : s = str1.split(" ") # split by space. now you can use loop to itearate each word and print first letter. edit: Imran Shakil an example for you : str1 = "the split way" for s in str1.split() : print( s[0], end="" ) this prints "tsw" for any further, share your try so that it helps where you struck...
12th Jan 2023, 11:05 AM
Jayakrishna 🇮🇳
+ 1
Imran Shakil it would help if we could see your code. How are you using split? Copy your code, open up a new code in the code playground, paste, save and attach it here please.
12th Jan 2023, 11:37 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
You need to readlines before you split them.
12th Jan 2023, 11:57 AM
Ausgrindtube
Ausgrindtube - avatar
0
Jayakrishna🇮🇳. I had never seen split() in my privious lessons.....I dont know how to use split(). This is the main problem
12th Jan 2023, 10:58 AM
Imran Shakil
Imran Shakil - avatar
0
file = open("/usercode/files/books.txt", "r") for line in file.readlines(): words = line.split() encode ='' for word in words : encode = encode + word[0] print (encode) file.close()
13th Jan 2023, 3:07 PM
BŁΛCKŠTØŔM
BŁΛCKŠTØŔM - avatar
12th Jan 2023, 11:10 AM
Imran Shakil
Imran Shakil - avatar
- 1
Jayakrishna🇮🇳. Every time when I had trid split(), it shows me AttributeError and shows a message "The file object dose not have a split attribute. You proberbly want to split the contents of the file into a list of line".
12th Jan 2023, 11:33 AM
Imran Shakil
Imran Shakil - avatar
- 1
Ausgrindtube #here it is file = open("/usercode/files/books.txt", "r") for i in file.split(): print(i[0],end="") file.close()
12th Jan 2023, 11:44 AM
Imran Shakil
Imran Shakil - avatar
- 1
Jayakrishna🇮🇳 Thank you very much. My exam have been completed by your help. I need your one more help...would you please tell me, where from I can learn albout split() by sololearn app?
12th Jan 2023, 12:26 PM
Imran Shakil
Imran Shakil - avatar