strip() test

0 Nathan Chambers · June 30, 2015
Is there a way to test is running stripe would remove and white space from either side?

for example (with a description in replace of an actual if statement)

a = " some string"
if a.strip() stripped from right:
    print("I had to remove blank space from the right")
elif a.strip() stripped from left:
    print("I had to remove blank space from the left")
else:
    print("Nothing had to be stripped, good job!")


I hope that example helps get what I'm asking. Because what I need it for, just doing strip isn't enough, I need to update an old datebase if the white space is found.

Post a Reply

Replies

Oldest  Newest  Rating
0 Halcyon Abraham Ramirez · June 30, 2015
if im not mistaken

rstrip() is to strip white space on the right
lstrip() is to strip white space on the left
0 Nathan Chambers · June 30, 2015
yes I know, but that is not what I'm asking. strip/rstrip/lstrip

I know how to remove it, but I need to know if there is a test to see if it actually did anything. Otherwise it would be a waste of resourced to UPDATE a row in a table, if strip didn't do anything. This is a huge database, that the last person never used strip on, so there are spaces all over.

like if text = "aaaaaa" strip did nothing, so dont update, if text = "                         huge space                         ", strip removed spaces, run UPDATE.

Maybe this examples it better? :)
0 Nathan Chambers · June 30, 2015
Err, I keep forgetting to use the code block lol... can u tell it's late at night :P

text = "no stripping needed"


vs


text = " huge white spaces on both sides... strip me "

1st  strip does nothing, so test would be False and I wouldn't need to do an SQL update
2nd would be removing space and be True, and trigger an SQL UPDATE.
0 Nathan Chambers · June 30, 2015
Or is there no built in test to see if strip actually did something, and I need to keep it like this?
text = "               some bad text                                    "
a = len(text)
text = text.strip()
b = len(text)

if b < a:
print("Run UPDATE because something was stripped")
0 Halcyon Abraham Ramirez · June 30, 2015
a = "              hellllooooooo                  "
b ="hello"

def space(letter):
    if letter != letter.strip():
        print("you must strip")
    else:
        print("no need to strip")
        
        
space(a)
space(b)



Im guessin that's what you wanted?
0 Nathan Chambers · June 30, 2015
Thank You  Halcyon Abraham Ramirez!
0 Halcyon Abraham Ramirez · June 30, 2015
Glad I could help :D
  • 1

Python

107,320 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator