Python Forum
getting first letters of a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: getting first letters of a string (/Thread-getting-first-letters-of-a-string)



getting first letters of a string - Unknown_Relic - Nov-12-2019

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)



RE: getting first letters of a string - perfringo - Nov-12-2019

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.