The answer has given something than expected

Hello all,
The mission is to identify a value starting and ending with [pdf] or [video]. But I have seen the answer it displays the values with [css], [Beta], [petition]. is it correct? Please give me some clarification.

Regular expression basics-10.matching at the start and end of the strings.

Thank you

My Code

beginning_count=titles[titles.str.contains(r'^\[[Pp]df|[Vv]ideo]\]')]
beginning_count_number=titles.str.contains(r'^\[[Pp]df|[Vv]ideo]\]').sum()
ending_count=titles[titles.str.contains(r'\[[Pp]df|[Vv]ideo\]$')]
ending_count_number=titles.str.contains(r'\[[Pp]df|[Vv]ideo\]$').sum()

DataQuest Answer:

pattern_beginning = r"^\[\w+\]"
beginning_count = titles.str.contains(pattern_beginning).sum()

pattern_ending =  r"\[\w+\]$"
ending_count = titles.str.contains(pattern_ending).sum()
1 Like

Hi @aditya1257,

  1. Count the number of times that a tag (e.g. [pdf] or [video] ) occurs at the start of a title in titles . Assign the result to beginning_count .
  2. Count the number of times that a tag (e.g. [pdf] or [video] ) occurs at the end of a title in titles . Assign the result to ending_count .

The instruction is not explicitly directing us to identify [pdf] or [video]. Those are just examples. We have to identify all tags []

Best,
Sahil

Thank you so much. I have taken the question too literally. :crazy_face: