+ 1

How do i read a file line by line then only return first character of each line?

Don't really know how to explain this. If you did the Sololearn python Word titles project. Since readlines() returns a list of strings from a file, how do i extract only the first characters of each 'list item'/element.

15th Mar 2021, 3:54 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
7 Answers
+ 2
LineOfString is a variable for a string value. Since strings are arrays of characters, you can access the individual characters by indexing (using [n]) a_string = "Hello" print(a_string[0]) # output H
15th Mar 2021, 4:02 PM
Slick
Slick - avatar
+ 1
Jan Markus lemme try this, thanks
15th Mar 2021, 4:02 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
+ 1
Use split function to add all the words to a list. Then iterate through all the items and print their first character.
15th Mar 2021, 4:03 PM
Peter Parker
Peter Parker - avatar
+ 1
Slick now thing is, readlines will return a list not string, so if i index a list like name[n], it will return first element of list, and if i use name[n][i] it will return first character of first item only, meanwhile i want it to return first characters of all items.
15th Mar 2021, 4:05 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
+ 1
Yeah a list of strings. When you have to preform the same operation over and over again use a loop.
15th Mar 2021, 4:06 PM
Slick
Slick - avatar
0
Universal would you mind to explain a lil bit about what LineOfString does?
15th Mar 2021, 3:58 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar