Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:54:45+00:00 2026-06-17T23:54:45+00:00

Sorry, another python newbie question. I have a string: my_string = <p>this is some

  • 0

Sorry, another python newbie question. I have a string:

my_string = "<p>this is some \n fun</p>And this is \n some more fun!"

I would like:

my_string = "<p>this is some fun</p>And this is \n some more fun!"

In other words, how do I get rid of ‘\n’ only if it occurs inside an html tag?

I have:

my_string = re.sub('<(.*?)>(.*?)\n(.*?)</(.*?)>', 'replace with what???', my_string)

Which obviously won’t work, but I’m stuck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:54:47+00:00Added an answer on June 17, 2026 at 11:54 pm

    You should try using BeautifulSoup (bs4), this will allow you to parse XML tags and pages.

    >>> import bs4
    >>> my_string = "<p>this is some \n fun</p>And this is \n some more fun!"
    >>> soup = bs4.BeautifulSoup(my_string)
    >>> p = soup.p.contents[0].replace('\n ','')
    >>> print p
    

    This will pull out the new line in the p tag. If the content has more than one tag, None can be used as well as a for loop, then gathering the children (using the tag.child property).

    For example:

    >>> tags = soup.find_all(None)
    >>> for tag in tags:
    ...    if tag.child is None:
    ...        tag.child.contents[0].replace('\n ', '')
    ...    else:
    ...        tag.contents[0].replace('\n ', '')
    

    Though, this might not work exactly the way you want it (as web pages can vary), this code can be reproduced for your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Sorry if it's a trivial question.) I have documents that looks like this (Python
Sorry if this is a repost of another question; I couldn't find anything after
Sorry, another super basic ASP.NET question. this so embarrassing. I am reading the article
Sorry, but I'm new to accessing return from another domain. I have this url
Sorry if this is a newbie question, I'm a bit rusty in Access and
Sorry to have to ask yet another position:fixed related question, but reading through various
Sorry, probably another really basic question. In my ViewScoped bean, a 'viewParam' looks like
Sorry if my last question is very similar, but I have another query of
Sorry another newbie question that I couldn't seem to find an answer to on
Sorry another newbie query :| To build upon the suggestion which was given here,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.