Home > Back-end >  How can I extract a word after # symbol in Python String
How can I extract a word after # symbol in Python String

Time:06-16

Given a sentence, e.g. "Im SHORTING #RSR here", I need to extract word that follow the "#" symbol (from, and not including the "#", to the next space).

Obviously, the "#" symbol can be anywhere in the string.

Thanks.

CodePudding user response:

You could use:

sentence = "Im SHORTING #RSR here"
words = [word.lstrip('#') for word in sentence.split() if word.startswith('#')]

The result will contain all hashtaged words in the sentence. If there's always exactly one, just use words[0]

CodePudding user response:

One pass solution (no in-built methods, no regex):

word= "Im SHORTING #RSR here"

res = []
for i in range(len(word)):
  if word[i] == "#":
    w = ""
    i  = 1
    while word[i] != " " and i < len(word):
      w  = word[i]
      i  = 1
    res.append(w)

print(res)     # [RSR]

Alternatively, you can use this regex: \B\#\w to get all words starting with #.

CodePudding user response:

Try this:

phrase = 'Im SHORTING #RSR here'

# split the input on spaces
words = phrase.split(' ')

# init empty list
comments = []

# iterate through each word
for word in words:
    # check if the first letter of the word is '#'
    if word[0] == '#':
        # add the comment to the list of comments
        comments.append(word)

# let's see what we have!
print(comments)
  • Related