+ 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.
7 Réponses
+ 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
+ 1
Jan Markus lemme try this, thanks
+ 1
Use split function to add all the words to a list. Then iterate through all the items and print their first character.
+ 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.
+ 1
Yeah a list of strings. When you have to preform the same operation over and over again use a loop.
0
Universal would you mind to explain a lil bit about what LineOfString does?