[Python] Keeping only the first word from a string | Python

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I am using this line below to extract text:

    country_abbreviation = columns[2].text.strip() # Country abbreviation

    The result I obtain is for example fr FRA. I would like to extract only fr, ignoring the second word FRA.

    I have tried using this method, but I get an error telling me there are no data to split:

    country_abbreviation = columns[2].text.strip().split()[0] # Country abbreviation

    Any idea where the issue might come from? Thank you!

    Continue reading...

Compartilhe esta Página