How to use def so that the input is a a variable?

I want to create a function where the input is a variable so I can make a function that will automatically remove letters and spaces from the variable. The input is like replaceletter(exvariable)? In python.

When you call a function you just put the variable name as the argument. For example:

someVar = 5

def addByFour(num1):
  return num1 + 4

addByFour(someVar) # That would return 9

This is what the function you are describing would look like:

def replaceLetters(exvariable):
    res = ""
    to_remove = list("abcdefghijklmnopqrstuvwxyz ")
    for c in exvariable:
        if c.lower() not in to_remove:
            res += c
    return res

print(replaceLetters("123 abc 456")) # -> 123456

Thank you, But I would prefer to use my own method. I just wanted help w/ the technical side. No offense.

Hi. I was also wondering if you could do something like variable=function(exvariable). This not showing on my Repl.

Yes, you can. I would recommend looking at this answer on stack overflow.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.