Python Forum
getting first letters of a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting first letters of a string
#1
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)
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing a words' letters in a string cananb 2 3,405 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Select single letters from string based on separate string Jpen10 3 2,630 Dec-15-2019, 01:21 PM
Last Post: Jpen10
  Counting the number of letters in a string alphabetically TreasureDragon 2 2,848 Mar-07-2019, 01:03 AM
Last Post: TreasureDragon
  Replacing all letters in a string apart from the first letter in each word Carbonix 9 4,833 Jan-17-2019, 09:29 AM
Last Post: buran
  Replacing letters on a string via location Owenix 2 2,438 Sep-16-2018, 10:59 AM
Last Post: gruntfutuk
  Amount of letters in a inputted string? CyanSupreme 3 3,581 May-10-2017, 09:40 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020