Jan-21-2021, 10:01 PM
i reformatted the code above
i input = " Testing a b c tab
output=Testingabc
i input = " Testing a b c tab
output=Testingabc
Help with removing spaces and tabs from a string
|
Jan-21-2021, 10:01 PM
i reformatted the code above
i input = " Testing a b c tab output=Testingabc
Jan-21-2021, 10:13 PM
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(""))
Jan-21-2021, 10:40 PM
(This post was last modified: Jan-21-2021, 10:44 PM by deanhystad.)
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.
Jan-21-2021, 10:42 PM
yea, because nothing is getting outputted.
Jan-21-2021, 10:48 PM
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
How do I use tabs and spaces? (how to resolve error TabError: inconsistent) | Onanism | 15 | 8,534 |
Mar-24-2022, 07:57 PM Last Post: Gribouillis |
|
Tab character in a string prints 8 spaces | hecresper | 6 | 26,794 |
Aug-27-2019, 02:38 PM Last Post: snippsat |
|
Removing dublicates from a string | JoeNancy | 6 | 4,722 |
May-20-2018, 12:55 PM Last Post: JoeNancy |
|
Removing string within string | fivestar | 2 | 3,849 |
Oct-20-2017, 04:30 AM Last Post: Skaperen |