## Question

Asked By – alvas

I have several alphanumeric strings like these

```
listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', '000alphanumeric']
```

The desired output for removing **trailing** zeros would be:

```
listOfNum = ['000231512-n','1209123100000-n','alphanumeric', '000alphanumeric']
```

The desired output for **leading** trailing zeros would be:

```
listOfNum = ['231512-n','1209123100000-n00000','alphanumeric0000', 'alphanumeric']
```

The desire output for removing both leading and trailing zeros would be:

```
listOfNum = ['231512-n','1209123100000-n', 'alphanumeric', 'alphanumeric']
```

For now i’ve been doing it the following way, please suggest a better way if there is:

```
listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', \
'000alphanumeric']
trailingremoved = []
leadingremoved = []
bothremoved = []
# Remove trailing
for i in listOfNum:
while i[-1] == "0":
i = i[:-1]
trailingremoved.append(i)
# Remove leading
for i in listOfNum:
while i[0] == "0":
i = i[1:]
leadingremoved.append(i)
# Remove both
for i in listOfNum:
while i[0] == "0":
i = i[1:]
while i[-1] == "0":
i = i[:-1]
bothremoved.append(i)
```

**Now we will see solution for issue: How to remove leading and trailing zeros in a string? Python **

## Answer

What about a basic

```
your_string.strip("0")
```

to remove both trailing and leading zeros ? If you’re only interested in removing trailing zeros, use `.rstrip`

instead (and `.lstrip`

for only the leading ones).

More info in the doc.

You could use some list comprehension to get the sequences you want like so:

```
trailing_removed = [s.rstrip("0") for s in listOfNum]
leading_removed = [s.lstrip("0") for s in listOfNum]
both_removed = [s.strip("0") for s in listOfNum]
```

This question is answered By – Pierre GM

**This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 **