How do you store a list of strings in an index function?

Question:

sentence = input("Input sentence: ")
punctuation = [" ", ",", ".", ":", "?", "!"]
interruption1 = sentence.index(punctuation)
word1 = sentence[:interruption1]
print(word1)

In this question, the main aim is to have the program print the first word that the user types by identifying a character that implies the first word has ended (the punctuation characters in the ‘punctuation’ variable). I want the program to accept the ‘punctuation’ variable within the index function but it sends an error message saying "must be str, not list". I tried for loops, they don’t work here either as far as I know.

A previous question of mine gave me information that you can’t use boolean values to represent a set of values in a variable, so I used a list, but now this error happens, and there is absolutely nothing on the Internet on this sort of problem (neither do I have an IT teacher or any friends that do Python), so I had to come here after about an hour of trying random combinations of code. How do I make Python accept the list and use it inside the index function? Thank you.

Asked By: ashton

||

Answers:

How do I make Python accept the list and use it inside the index function?

You can’t str.index only accepts str.

I tried for loops, they don’t work

A for loop could work

  • iterate over sentence while keeping track of the index
  • for each character check to see if it is in punctuation
    • if it isn’t in punctuation
      • continue with the next character
    • if it is in punctuation
      • use the index to extract the first word using a slice: sentance[:index]
      • stop iterating
Answered By: wwii

To be honest this is something you should do with regular expressions. But, that doesn’t really answer your question, so – you are close, but your problem is that you’re passing a list of strings instead of a single string, as your error message implies.

You should loop through each string in the list and get the first occurrence of the string in your sentence. You can use str.index() but I prefer to use str.find() which will return a -1 if the character is not found thus we don’t have to mess around with error exceptions.

sentence = input("Input sentence: ")
punctuation = [" ", ",", ".", ":", "?", "!"]

matches = []

for character in punctuation:
    match = sentence.find(character)

    #If the character is found, save to a separate list. 
    #No match means value is -1, so ignore it using >0 

    if match >0:
        matches.append(match) 

# Now find the match that occurs first:

first_match_index = min(matches)

# Return first word

first_word = sentence[:first_match_index]
print(first_word)
Answered By: suryaya

You don’t need to use an index unless it is demanded; just keep accepting characters from the sentence until you come to a punctuation. So the simplest looping approach is:

sentence = input("Input sentence: ")

punctuation = [" ", ",", ".", ":", "?", "!"]

result = ""
for c in sentence:
    if c in punctuation:
        break
    else:
        result = result + c

print(result)
Answered By: user19077881
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.