I want to add space between number and text
Example string: ABC24.00XYZ58.28PQR
output: ABC 24.00 XYZ 58.28 PQR
Please let me know the answers.
Thanks a lot.
You can use re.split
to separate the input string into a list of tokens. Then join all those tokens by a space.
import re
s = "ABC24.00XYZ58.28PQR"
split = [c for c in re.split(r'([-+]?\d*\.\d+|\d+)', s) if c]
result = " ".join(split)
print(result)
Output:
ABC 24.00 XYZ 58.28 PQR
The regex r'([-+]?\d*\.\d+|\d+)'
should be fairly robust and detect floats of the type -12
, +5.0
as well.
If there is no more requirements,you could use regex:
import re
s = "ABC24.00XYZ58.28PQR"
s = re.sub("[A-Za-z]+",lambda group:" "+group[0]+" ",s)
print(s.strip())
Concatenate string and converted number to string type:
print ("AB" + " "+ str(34)) //or
print ("AB " + str(34))
If you want to add spaces in string use Regex refer: python regex add space whenever a number is adjacent to a non-number