How to remove spaces at the beginning of a name

Question:

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
Asked By: sefin

||

Answers:

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

Answered By: birmsi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.