Search code examples
pythonreplacesplitwhitespace

How to delete leading and trailing characters from string?


I have this code:

    for item in items:
        currency_name = item.text
        currency_name.split('\n')
        comps.append({
            'currency_name': currency_name
        })

    print(comps)

and this is my print(comps):

{'currency_name': '\n\n                     Австралійський долар                   \n'}, {'currency_name': '\n\n                     Азербайджанський манат                   \n'},

How else can I delete this \n and space?


Solution

  • Try str.strip:

    ...
    
    currency_name = item.text
    comps.append({
        "currency_name": currency_name.strip()
    })
    
    ...