Assumed I have string : 0²0 36 0 ² 6 4
How can I convert it into string : 020360264
Assumed I have string : 0²0 36 0 ² 6 4
How can I convert it into string : 020360264
s = "0²0 36 0 ² 6 4"
s = s.replace("²", "2") # replace the funky 2
s = "".join(c for c in s if c.isdigit()) # keep digits only
print(s)
Outputs, as expected, 020360264
this is a variant:
strg = "0²0 36 0 ² 6 4"
strg = strg.translate(str.maketrans({"²": "2", " ": None}))
# 020360264
using str.maketrans
and str.translate
to remove all the spaces and convert '²'
to '2'
.