Python Forum
Trying to get the first letter of every word in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get the first letter of every word in a list
#1
I am trying to take out the first letter of every word in a list. It is not working. I believe the fault to be on line 9 but i can't figure out why.

def initialise(userText):
    lst = []
    firstLetters = []
    for i in userText:
        lst.append(i)
    firstLetters.append(lst[0])
    for x in lst:
        if x == " ":
            position = lst.index(x) + 1
            firstLetters.append(lst[position])
    userText = ''.join(firstLetters)
    return userText


text = input("Enter text: ")
print(initialise(text))
Reply
#2
Isin't the objective to get first letter of every word in a string? If this is the case then simple 'split on space and get first letter' comprehension would do:

>>> text = 'Life, Universe and Everything'
>>> ''.join(word[0] for word in text.split())
'LUaE'
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
#3
Getting back to the original post question, index() will not work because you can have repeating letters. You could use enumerate to iterate through the user text.
def initialise(userText):
    firstLetters = userText[0]
    for index, letter in enumerate(userText):
        if letter == " ":
            firstLetters += userText[index+1]
    return firstLetters
A problem with this approach is it assumes the next letter after a space is the start of a word. This fails for us older folks who follow a period with two spaces, and it would crash if you entered a trailing space.

A safer solution is to set a flag to let you know that you are in a break and looking for the start of the next word.
def initialise(userText):
    punctuation = ' .,:;()"'
    firstLetters = ''
    start = True
    for letter in userText:
        if letter in punctuation:
            start = True
        elif start:
            firstLetters += letter
            start = False
    return firstLetters
 
print(initialise('This is a test.  It works for (some) punctuaton.'))
bowlofred likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to find first 2 letter word in a list of words Oldman45 7 1,066 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 790 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Python Speech recognition, word by word AceScottie 6 10,653 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 1,658 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 1,543 Oct-17-2019, 03:04 PM
Last Post: baquerik
  returns index of list if contains a word zarize 0 711 Sep-09-2019, 09:29 AM
Last Post: zarize
  Score each word from list variable pythonias2019 6 1,350 Jun-13-2019, 05:44 PM
Last Post: gontajones
  Creating a word list opencircles 2 3,419 Mar-19-2019, 06:00 PM
Last Post: MohanReddy
  Linear search/searching for a word in a file/list kietrichards 3 1,743 Mar-08-2019, 07:58 PM
Last Post: Larz60+
  Replacing all letters in a string apart from the first letter in each word Carbonix 9 2,477 Jan-17-2019, 09:29 AM
Last Post: buran

Forum Jump:

User Panel Messages

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