0

I am trying to implement my own strip method in Python, so without using the built-in method, I'd like my function to strip out all the whitespace from the left and the right.

Here, what I am trying to do is create a list, remove all the blank character before the first non-space character, then do it reverse way, finally return the list to a string. But with what I wrote, it doesn't even remove one whitespace.

I know what I am trying to do might not even work, so I would also like to see the best way to do this. I am really new to programming, so I would take any piece of advise that makes my program better. Thanks!

# main function
inputString = input("Enter here: ")
print(my_strip(inputString))

def my_strip(inputString):
    newString = []
    for ch in inputString:
        newString.append(ch)
    print(newString)
    i = 0
    while i < len(newString):
        if i == " ":
            del newString[i]
        elif i != " ":
            return newString
        i += 1
    print(newString)
Yolanda
  • 17
  • 6

3 Answers3

3

Instead of doing a bunch of string operations, let's just get the beginning and ending indices of the non-whitespace portion and return a string slice.

def strip_2(s):
    start = 0
    end = -1
    while s[start].isspace():
        start += 1
    while s[end].isspace():
        end -= 1
    end += 1
    return s[start:end or None]
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 1
    Minor note: Providing `None` is equivalent to not filling in that part of the slice. So you could convert `end` of `0` to `None` (to grab until the end of the string) to slice to end of string more succinctly with `return s[start:end or None]`; if `end` is negative, it's used, if it's `0`, it uses `None` instead to slurp to the end of the string. – ShadowRanger Oct 26 '16 at 01:11
  • @ShadowRanger I feel like i've learned more cool little python tricks in comments on my answers than anywhere else – Patrick Haugh Oct 26 '16 at 01:13
  • Need to bound check start and end. start – Majnu Jan 10 '21 at 22:28
0

How about using regular expression?

import re

def my_strip(s):
   return re.sub(r'\s+$', '', re.sub(r'^\s+', '', s))

>>> my_strip('   a c d    ')
'a c d'
Enix
  • 4,415
  • 1
  • 24
  • 37
  • 3
    I feel like trying to "implement it themselves" precludes cheats like regexes. I mean, sure, you can do it, but it's not much of a learning exercise. Side-note: You could save a little work by combining into one regex using alternation: `return re.sub(r'^\s+|\s+$', '', s)` – ShadowRanger Oct 26 '16 at 01:27
0

What you seem to be doing is an ltrim for spaces, since you return from the function when you get a non-space character.

Some changes are needed:

# main function
inputString = input("Enter here: ")
print(my_strip(inputString))

def my_strip(inputString):
    newString = []
    for ch in inputString:
        newString.append(ch)
    print(newString)
    i = 0
    while i < len(newString):
        if i == " ": # <== this should be newString[i] == " "
            del newString[i]
        elif i != " ": # <== this should be newString[i] == " "
            return newString
        i += 1 # <== this is not needed as the char is deleted, so the next char has the same index
    print(newString)

So the updated code will be:

# main function
inputString = input("Enter here: ")
print(my_strip(inputString))

def my_strip(inputString):
    newString = []
    for ch in inputString:
        newString.append(ch)
    print(newString)
    i = 0
    while i < len(newString):
        if newString[i] == " ":
            del newString[i]
        elif newString[i] != " ":
            return newString
    print(newString)

Good luck with the rest of the exercise (implementation of rtrim).

user1952500
  • 6,611
  • 3
  • 24
  • 37