Issue
I am looking for a script code to search for the first 3 words of a string.
Example :
words = 'I am confused looking for 3 words from the front'
Expected results:
'I am confused'
Solution
You can split the string into a list using the .split()
method. Once you've done this you can extract the first 3 words from the sentence using a list slice ([:3]
). Finally you'll want to join the result back together into a new string using .join()
:
words = 'I am confused looking for 3 words from the front'
' '.join(words.split()[:3])
>> 'I am confused'
Answered By - AK47
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.