Reputation: 23
Not sure how to do this. I have a string that I need the first part of it gone. When print(result.text) runs it prints "@PERSONSTWITTER their message" I need to remove the first part "@PERSONSTWITTER".
At first I had it remove everything from the @. I ran into a problem, the first is that the person username could be any amount of letters. (@PERSONSTWITTER2, @PERSONSTWITTER12, etc) they don't have the same amount of characters. Now I'm not sure what to do. Any help would be great!
So all I need is to isolate "their message" and not the username.
for s in twt:
sn = s.user.screen_name
m = "@%s MESSAGE" % (sn)
s = api.update_status(m, s.id)
#time.sleep(5)
for result in twt:
print(result.text)
Upvotes: 0
Views: 72
Reputation: 21
s = "@PERSONSTWITTER their message"
split_s = s.split(' ')
message = ' '.join( split_s[1:] )
Upvotes: 0
Reputation: 48057
You may filter the words starting with @
using string.startswith
as:
>>> s = "@PERSONSTWITTER their message. @ANOTHERWRITER their another message."
>>> ' '.join(word for word in s.split() if not word.startswith('@'))
'their message. their another message.'
Here I'm firstly splitting your sentence into words, filtering the words not starting with @
, and then joining back again.
Upvotes: 1
Reputation: 20414
Use the .split()
method to convert the string
into a list
of strings
formed from splitting the original at (by default) spaces
.
Then use the .join
method to join all the elements from index 1
on wards in the list together, separated again by a space.
s = "@PERSONSTWITTER their message"
' '.join(s.split()[1:])
# --> 'their message'
An alternative approach would be to just index
the first space
and slice from then on wards:
s = "@PERSONSTWITTER their message"
s[s.index(' ')+1:]
# --> 'their message'
Note that we had to add 1
to the index because strings
are zero-based
Upvotes: 0
Reputation: 71451
You can use regular expressions:
import re
s = "@PERSONSTWITTER their message"
new_s = re.sub('^\S+', '', s)[1:]
Output:
'their message'
Upvotes: 1