Thursday, September 17, 2009

Python - string stripping


# string manipulations
# striping characters out of strings

aUrl = "http://www.example.com"

# strip, lstrip, rstrip only remove characters
#   on the either edge (strip), the left edge (lstrip),
#   and the right edge(rstrip)
print aUrl.strip("htp:/w.com")

#outputs:
#   example

print aUrl.lstrip("htp:/")

#outputs:
#   www.example.com

print aUrl.rstrip(".com")

#outputs:
#   http://www.example

# to strip out all occurances
#   of a character use replace()
print aUrl.replace('m', "")

#outputs:
#   http://www.exaple.co

No comments:

Post a Comment