Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove spaces at the beginning of a name

I have a dollar rate parser, but when displaying the name of the currency, spaces are also displayed before its name, how to remove them?

def usd():
    r = requests.get('https://fx-rate.net/USD/')

    if r.status_code == 200:
        soup = bs4(r.text, "html.parser")
        tbody = soup.find_all("tbody")[1]

        for info in tbody:
            try:
                name = info.find("td").text
                rate = info.find("a", class_ = "1rate").text

            except:
                pass
        
            print(f"{name} | {rate}")

    else:
        print(r.status_code)
usd()

Example output:

   Danish Krone | 7.65
   Egyptian Pound | 19.7
   Ethereum | 0.0008
   Euro | 1.03
   Hong Kong Dollar | 7.85

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try:
print(f"{name.strip()} | {rate}")

strip() will remove leading and trailling whitespaces.

Refer to this guide for deeper explanation:
https://www.digitalocean.com/community/tutorials/python-trim-string-rstrip-lstrip-strip

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading