Python Forum
Help with removing spaces and tabs from a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help with removing spaces and tabs from a string (/thread-32098.html)

Pages: 1 2


RE: Help with removing spaces and tabs from a string - msqpython - Jan-21-2021

i reformatted the code above
i input = " Testing a b c tab
output=Testingabc


RE: Help with removing spaces and tabs from a string - msqpython - Jan-21-2021

Sorry about that. I accidentally posted the code twice, there was a version above the last post that had the correct format but I'll paste it here again with the correct format. My program is supposed to return the exact same string that the user gives except with all spaces and tabs removed. Here is my program without the trim function..

newstring =""
string = input("Enter a string: ")
pos = 0
length = len(string)
while (pos<length):
    if (string[pos].isspace()!=True):
        newstring = newstring + string[pos]
    pos += 1
print(newstring)
And this is the version that I changed in order to have it all in a (trim) function.

string = input("Enter a string: \n")

 

def trim(string):

    newstring = ""

    pos = 0 

    length = len(string)

    

    while (pos < length

       ): 

       if (string[pos].isspace() != True

        ):

        newstring = newstring + string[

            pos] 

    pos += 1 

 

  

 

print(trim(""))



RE: Help with removing spaces and tabs from a string - deanhystad - Jan-21-2021

Did you read about Python functions yet. The reason you get "None" back from your function should be really obvious if you did. It is a common mistake.

Nice to see you haven't followed anybody's suggestions.


RE: Help with removing spaces and tabs from a string - msqpython - Jan-21-2021

yea, because nothing is getting outputted.


RE: Help with removing spaces and tabs from a string - deanhystad - Jan-21-2021

https://www.geeksforgeeks.org/python-return-statement/