Skip to content
Advertisement

Advanced string manipulation in Python

I’m trying to get only the value that are after the string 2021 from the following string:

           Revenue     Earnings
Year                           
2018  110360000000  16571000000
2019  125843000000  39240000000
2020  143015000000  44281000000
2021  168088000000  61271000000

I need to get those 2 values separated (first one has to be 168088000000 and the second one has to be 61271000000 in this case).

They have to be preceded by 2021 (and the result should give only the 2 numbers I mentioned above so for example if there is a value 2022 and others numbers they should not be taken).

The result should be a=168088000000 b=61271000000 both as strings.

Advertisement

Answer

I think you can get inspired by this:

text = """
2018  110360000000  16571000000
2019  125843000000  39240000000
2020  143015000000  44281000000
2021  168088000000  61271000000
"""

row_split = text.split("n")

for row in row_split:
    col = row.split("  ")
    if col[0] == "2021":
        a = col[1]
        b = col[2]
        print(a, b)
        break
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement