add tag
Given `<p>Bananas <em>in</em> pyjamas are <em>going</em> down the <em>stairs</em>.</p>`

How can I get the HTML string inside the `<p>` tag?

That is, `Bananas <em>in</em> pyjamas are <em>going</em> down the <em>stairs</em>.`

paragraph = '<p>Bananas <em>in</em> pyjamas are <em>going</em> down the <em>stairs</em>.</p>'
paragraphTag = BeautifulSoup(paragraph, "html.parser")
# paragraphTag.???
Top Answer
The `BeautifulSoup` constructor creates an object that represents the whole HTML document. Of course in this case, the document contains only one `p` tag, but anyway, your solution also outputs the `<p>` and `</p>` tags, which is not what you wanted (if I understood correctly).

To get only the tag contents (but not the opening and closing tags themselves), you should use `find` to get only the tag, and then use `decode_contents()` to get all tag contents as a string:

from bs4 import BeautifulSoup

paragraph = '''<p>Bananas <em>in</em> pyjamas are <em>going</em> down the <em>stairs</em>.</p>'''

# get a "soup" object that represents the whole HTML document
soup = BeautifulSoup(paragraph, "html.parser")

# get only the p tag
paragraphTag = soup.find('p')

# get tag contents as a string
print(paragraphTag.decode_contents()) # Bananas <em>in</em> pyjamas are <em>going</em> down the <em>stairs</em>.
Answer #2
It seems like there is no direct way to retrieve the string inside a tag.

`.text` and `.strings` will strip the tags.

`.contents` is our best bet but you have to cast to a string first.

return "".join(map(str, paragraphTag.contents))

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.