Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
I have the doubt, in the python each string is, Z = "00123+0567*29/03-7"

how to convert it to a "123+567*29/3-7"

Even I tried with the re.split('[+]|[*]|-|/', Z) later with for i in res : i = i.lstrip("0") but it will split it correctly, but to join it back with the same operands as in the string "Z" as Z = "123+567*29/3-7"

How to solve it

1 Answer

0 votes
by (36.8k points)

Use the below code:

def cut_zeroes(Z):

    i, res = 0, []

    n = len(Z)

    while i < n:

        j = i

        while i < n and Z[i] not in '+-/*':

            i += 1

        res.append(int(Z[j:i]))

        if i < n:

            res.append(Z[i])

        i += 1

    return ''.join(map(str,res))

  

Z = "00123+0567*29/03-700"

print(cut_zeroes(Z))

 Want to be a master in Data Science? Enroll in this Data Scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...