Home > katas > Remove String Spaces (8kyu) [Python]

Remove String Spaces (8kyu) [Python]

Chek kata on Codewars


Simple, remove the spaces from the string, then return the resultant string.

Solution 1.1

Let's start with a loop solution.

def no_space(x):
    char = ''
    for i in range(len(x)):
        if x[i] != ' ':
            char = char + x[i]

    return char

Solution 1.1+

def no_space(x):
    char = ""
    for i in x:
        if i.isspace() == False:
            char += i
    return char

Solution 2.1

Let's solve it with replace.

def no_space(x):
    return x.replace(" ", "")

Solution 3.1

Let's solve it with split and join.

def no_space(x):
    return "".join(x.split())

Solution 3.2

Let's solve it with filter.

def no_space(x):
    return ''.join(filter(lambda char: not char == " ", x))
© 2021, Andrew Losseff