+ 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
11 Antworten
+ 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)
+ 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)
+ 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)
+ 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)
+ 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
Thanks
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)
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