Skip to content

Instantly share code, notes, and snippets.

@naranjja
Last active January 12, 2019 01:01
Show Gist options
  • Save naranjja/8a6e9ccf14d1076d36a793818dbe9a98 to your computer and use it in GitHub Desktop.
Save naranjja/8a6e9ccf14d1076d36a793818dbe9a98 to your computer and use it in GitHub Desktop.
Trim padding on string
import re
def remove_padding(char, s, left=None, right=None):
if not left and not right:
print("You must specify either left or right.")
return
elif left:
return re.findall(re.escape(char) + r"*(.+)", s)[0]
elif right:
return re.findall(re.escape(char) + r"*(.+)", s[::-1])[0][::-1]
s = "123000000"
s_trimmed = remove_padding("0", s, right=True)
print(s_trimmed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment