Q:

Replace number by string in text file

Given a string as input, I want to find and convert the integer elements of the line with the string num. Here is an example:
input: 
"abc 123 def 1 in 1s and 100MB"

output:
"abc >num< def >num< in 1s and 100MB"

How can I do it?

A:

That is how.
text_in = "abc 123 def 1 in 1s and 100MB"
text_out = ' '.join(['>num<' if x.isnumeric() else x for x in text_in.split()])
print(text_out)  # prints: abc >num< def >num< in 1s and 100MB

I took the liberty to assume that you can read the text from a file and can also write the new text to a file so I am skipping those parts.
It all boils down to the use of isnumeric().

A:

Use a regular expression:
import re

s = "abc 123 def 1 in 1s and 100MB"

print(re.sub(r"\b\d+\b", ">num<", s))
# output: "abc >num< def >num< in 1s and 100MB"

The regular expression string r"\b\d+\b" matches a character string that consists of at least one numeral character (\d+) enclosed by word boundaries (\b). The \b part also matches the beginning and the end of a string.
The call to re.sub() replaces all occurrences of a matching character string in the third argument s  by the second argument (">num<").

