Python Forum

Full Version: getting first letters of a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
im new to python and im trying to split a string so it prints the first letter of each word.
Below is the error i keep getting

line 8, in split_song
for letter in songnames.split(" "):
AttributeError: 'function' object has no attribute 'split'

def access_data():
    with open("Songnames.txt", "r") as f:
        songnames = [line.strip("\n") for line in f]


def split_song(songnames):
    for letter in songnames.split(" "):
        print(letter[0])

songnames = access_data
split_song(songnames)
There are several things you should take care of:

- if you want to call function then you need to use () i.e songnames = access_data()
- if function body doesn't return (or yield) anything it returns None (and your function does the latter)

So even if you fix your first problem you will run into second.

EDIT: if you fix your code you should also reconsider your approach. It's unclear what is you objective - 'first letter of a string' (as in thread name) or 'first letter of each word' in thread itself.