Python Forum

Full Version: how do one remove whitespace from a list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I'm trying to make a programme that calculates a mathematical ratio problem. in order to do so i convert the 4 numbers the user enters into list elements, then i perform the operations on them. However, i have a problem with whitespace, how do i remove those in python? Here's my programme:

# !/usr/bin/python
# -*- coding: utf8 -*-
import math


def data_prosesering():
    
    ratio = list(raw_input('ratio syfers: '))

    r1 = float(ratio[0]) * float(ratio[1])
    r2 = float(ratio[2]) * float(ratio[3])

    a = r1/r2

    print math.sqrt(a)


data_prosesering()
First searched and tried to do something like this: d = [x.strip() for x in ratio]
Then however i got stuck so i went searching for solutions again and finally got the idea of trying this:
    pre_ratio = raw_input('ratio syfers: ')
    pre_ratio.split()
    ratio = list(pre_ratio)
    
    print ratio #testing if this works
This however didn't work either. Does anyone know how to solve this problem?

Python version: 2.7
operating system: windows 8
You can use a regular expression

import re

pre_ratio = re.sub(r'\s', '', pre_ratio)
list(raw_input().replace(' ', ''))
hee ll o
['h', 'e', 'e', 'l', 'l', 'o']
Ah yes of course! thanks guys, you solved a lot of struggle for me now :)