Trying to solve social media pro in Python core | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

17th Apr 2021, 2:36 PM
Simisola Osinowo
Simisola Osinowo - avatar
8 Answers
+ 11
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)
17th Apr 2021, 3:15 PM
Lothar
Lothar - avatar
+ 10
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)
17th Apr 2021, 3:01 PM
Ervis Meta
Ervis Meta - avatar
+ 5
Here are 2 solutions- Without regex: 1. text = input() #your code goes here tx=text.split(" ") for wor in tx: if wor[0]=="#": print(wor) 2. Using regex: import re text = input() #your code goes here #use re.findall() with r"#\w+" as the regex pattern = r"^#" tx=text.split(" ") for wor in tx: if re.findall(pattern, wor): print(wor)
25th Apr 2022, 2:28 AM
Shubhang
+ 2
this is my code, you can try import re text = input() #your code goes here #use re.findall() with r"#\w+" as the regex centro= r"#\w+" lista= re.findall(centro,text) if lista: for x in lista: print(x)
30th May 2022, 8:15 PM
Jhonatan Pereda Azabache
+ 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 !
17th Apr 2021, 3:09 PM
Ervis Meta
Ervis Meta - avatar
0
Thanks
19th Apr 2021, 6:24 AM
Simisola Osinowo
Simisola Osinowo - avatar
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)
9th Oct 2021, 9:21 AM
Raoof Zakarna
0
import re text = input() #your code goes here pattern = r"#\w+" match = re.findall(pattern,text) if match : for i in match: print(i) #use re.findall() with r"#\w+" as the regex
26th Nov 2022, 9:35 AM
Pareshkumar Chaudhari