Python Forum

Full Version: convert integers to a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def convert_ceiling(text):
    split = text.split()
    key = 'FEW', 'OVC', 'BKN', 'SCT'
    counter = 0
    for s in split:
        counter += 1
        for k in key:
            if k in s:
                index = split[counter - 1]
                altitude = index.replace(k, '')
                altitude = int(altitude) * 100
                print(altitude)
When I print(altitude), I'm getting:

Quote:1700
900
2400
20000
500

How can I convert those integers into a string with a space so they will be

1700 900 2400 20000 500

I tried
altitude.replace('\n',' ')
but that didn't work. I'm assuming that's because they're integers. I changed them to str, but still not working. I'm not reading a text file just to be clear, I'm getting these data from a website.

And when I call the function, I'm getting 'None'
w = 'KORD 031551Z 28011G18KT 10SM FEW017 OVC009 BKN024 BKN200 17/13 SCT005 A3003 RMK AO2 SLP168 T01720133'
print(convert_ceiling(w))
print(altitude, end = ' ') will display them with a space in between. However, if you want that in a variable, you'll need to make a list of the string versions of the int, and use the join method of the list.