How can I grab a group of words from a string?

James Dean

I have a string that is a sentence. There are eight words in the sentence. What I'm trying to do, is take the third, forth, and fifth word the sentence. I have tried using indexing such as:

string[3][4][5]

But this raises an IndexError. What am I missing here?

omijn
# split the title string into words (split by spaces)
thead_list = page_soup.title.string.split()

# access elements with index 3, 4, 5
words = thead_list[3:6]

Or if you want just the third and fifth words, use thead_list[2] and thead_list[4]

If you need to concatenate the resulting words that you extracted, then do this:

new_title = " ".join(words) # converts ["word1", "word2"] to "word1 word2"

Combining all of the above steps into one line of code:

thead = " ".join(page_soup.title.string.split()[3:6])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I select a string from between specified words

From Dev

How i can get specific words from string?

From Dev

How can I detect multiple words from a string? (python)

From Dev

How can I remove the special character words from a string?

From Dev

Grab random sequence of words from a string?

From Dev

How can I grab the last element of a List of string arrays?

From Dev

How can I grab the last element of a List of string arrays?

From Dev

How can i grab specific string inside the brackets

From Dev

How can I split a string by words?

From Dev

Regex from a html parsing, how do I grab a specific string?

From Dev

How can I grab a streaming video manifest from a web page?

From Dev

Git basics, How can I grab files from git?

From Dev

How can i extract words from a string before colon and excluding \n from them in python using regex

From Dev

How can I extract an exact set of words from a string using a regex?

From Dev

How can I split commas and periods from words inside of string using split?

From Dev

How do I grab the part of this string after a `/`?

From Dev

How do I grab entries with common values for one element from a List<KeyValuePair<string, string>>?

From Dev

How can I grab the rows from a specific table using an XPATH expression - mine seems to grab all the rows in the document

From Dev

How can I chunk a string in JavaScript respecting words?

From Dev

How can I replace words in a string with corresponding to the order of a vector?

From Dev

How can I display unique words contained in a Bash string?

From Dev

How can I check whether a string of words exists in php

From Dev

How can I put the individual words of a string in a javascript array?

From Dev

How can I check a string of words to see if they are an anagram of one word

From Dev

How can I make words starting with # or @ string act like UIButton?

From Dev

How I can reverse multi words in a String by Stack keeping in order

From Dev

How can I extract words before some string?

From Dev

How can I remove specify string words with php

From Dev

Lua - How can I grab any return?

Related Related

  1. 1

    How can I select a string from between specified words

  2. 2

    How i can get specific words from string?

  3. 3

    How can I detect multiple words from a string? (python)

  4. 4

    How can I remove the special character words from a string?

  5. 5

    Grab random sequence of words from a string?

  6. 6

    How can I grab the last element of a List of string arrays?

  7. 7

    How can I grab the last element of a List of string arrays?

  8. 8

    How can i grab specific string inside the brackets

  9. 9

    How can I split a string by words?

  10. 10

    Regex from a html parsing, how do I grab a specific string?

  11. 11

    How can I grab a streaming video manifest from a web page?

  12. 12

    Git basics, How can I grab files from git?

  13. 13

    How can i extract words from a string before colon and excluding \n from them in python using regex

  14. 14

    How can I extract an exact set of words from a string using a regex?

  15. 15

    How can I split commas and periods from words inside of string using split?

  16. 16

    How do I grab the part of this string after a `/`?

  17. 17

    How do I grab entries with common values for one element from a List<KeyValuePair<string, string>>?

  18. 18

    How can I grab the rows from a specific table using an XPATH expression - mine seems to grab all the rows in the document

  19. 19

    How can I chunk a string in JavaScript respecting words?

  20. 20

    How can I replace words in a string with corresponding to the order of a vector?

  21. 21

    How can I display unique words contained in a Bash string?

  22. 22

    How can I check whether a string of words exists in php

  23. 23

    How can I put the individual words of a string in a javascript array?

  24. 24

    How can I check a string of words to see if they are an anagram of one word

  25. 25

    How can I make words starting with # or @ string act like UIButton?

  26. 26

    How I can reverse multi words in a String by Stack keeping in order

  27. 27

    How can I extract words before some string?

  28. 28

    How can I remove specify string words with php

  29. 29

    Lua - How can I grab any return?

HotTag

Archive