Trying to solve social media pro in Python core | Sololearn: Learn to code for FREE!

+1

Trying to solve social media pro in Python core

You are a social media marketing specialist doing research on social networks. Write a program for your research that will take text as input and output all of the hashtags in it separately. Sample Input No #pressure, no #diamonds Sample Output #pressure #diamonds How do i output the word on different lines My code import re text = input() pattern=r"#\w+" if re.findall(pattern,text): print("\n") #your code goes here #use re.findall() with r"#\w+" as the regex

4/17/2021 2:36:04 PM

Simisola Osinowo

5 Answers

New Answer

+9

Simisola Osinowo , the issue in your current code is, that: ▪︎if re.findall(pattern,text): checks if there is a match with "#..." (True or False), but you are missing to get the result of the regex as a list: ▪︎result = re.findall(pattern,text): result is a list that contains all words that start with "#". to output the elements of the list, you can do it like Ervis Meta mentioned, but you can also use a simple for loop to do this: for i in result: print(i)

+6

If you see closer, the .finall method returns a list doesn't it ? So why don't you try to join this list like this : import re text = input() pattern = r'#\w+' search = re.findall(pattern,text) result = '\n'.join(search) #joins results with a newline print(result)

+1

Because the way you have chosen sure will find the .findall method with a number of correct results, BUT you have forgotten to see what will be printed if .findall returns any thing at all !

0

0

this code ; import re text = input() pattern = r'#\w+' search = re.findall(pattern,text) result = '\n'.join(search) #joins results with a newline print(result)