Skip to content

Remove leading and trailing characters

If you work with data like IDs, you may want to remove leading and/or trailing characters or numbers from a string. Python provides methods for that, called str.strip():

id = "0000123456000"
leading_removed = id.lstrip("0") # leading_removed is "123456000"
trailing_removed = id.rstrip("0") # trailing_removed is "0000123456"
both_removed = id.strip("0") # both_removed is "123456"


Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.