Home > OS >  how to remove all zeros that are 7 characters long python
how to remove all zeros that are 7 characters long python

Time:07-02

I have made a string without spaces. so instead of spaces, I used 0000000. but there will be no alphabet letters. so for example, 000000020000000050000000190000000200000000 should equal "test". Sorry, I am very new to python and am not good. so if someone can help me out, that would be awesome.

CodePudding user response:

Unless I'm missing some context around your question, you are able to use the replace function to do this. It should be as simple as something like this:

my_string = '000000020000000050000000190000000200000000'

new_string = my_string.replace('0' * 7, '')

To get the literal word "test" you could even do something like replacing each '0000000' with an actual space, and then splitting it by a space. Your code might look something like this:

new_string = my_string.replace('0' * 7, ' ')
characters = new_string.trim().split()

The characters array would then be ['20', '5', '19', '20'] which you can then convert to the alphabetical characters

  • Related