4

One of my modules (t_mesg.py) has a multi-line string:

tm = """
this
is currently
a
test
message
"""

I import this in another module where I would need to replace certain parts of the string with others. However, on the import, the newline chars come in as well and so tm.replace(...) would not work.

>>> from t_mesg import tm
>>> tm
'\nthis\nis \na\ntest\nmessage\n'

If I need to process this imported string to change "is" to "is not" how would I go about it, so that the string looks like this?

tm = """
this
is not currently
a
test
message
"""

TL;DR - how can I perform the replacement ignoring the newline chars?

7
  • 2
    you're witnessing the representation of the string. But it will work ok. Try print(tm) Commented Sep 20, 2018 at 14:33
  • It's not clear to me, are you asking how to create a multi-line string without newlines? Or how to perform the replacement ignoring the new-lines? or something else? Commented Sep 20, 2018 at 14:35
  • 2
    tm.replace('\nis \n', '\nis not\n') works fine for me. Of course, it doesn't modify tm, since strings are immutable. If you want to change tm, you need tm = tm.replace('\nis \n', '\nis not\n') Commented Sep 20, 2018 at 14:36
  • 1
    probably a regex then Commented Sep 20, 2018 at 14:38
  • 2
    what do you mean by "ignoring the newline chars" Commented Sep 20, 2018 at 14:38

3 Answers 3

10

Basically you want to perform a word replacement in a string. You can do it using regular expressions & word boundaries, and the hell with newlines or not:

import re
s = "this\n is \n a good \n question"
s = re.sub(r"\bis\b","is not",s)
print(s)

result:

this
 is not 
 a good 
 question

You can revert back with this (which allows some more newlines to be present between both words and preserves them)

s = re.sub(r"\bis\b(\s+)\bnot\b",r"is\1",s)
print(s)

prints:

this
 is 
 a good 
 question

to go a little further, you could introduce punctuation and other non-alpha stuff and using \W you could still manage:

s = "this\n is - not - \n a good \n question"
s = re.sub(r"\bis(\W+)not\b",r"is\1",s)
print(s)

prints ("not" has gone away but not the dash before it):

this
 is -  - 
 a good 
 question
Sign up to request clarification or add additional context in comments.

1 Comment

This is brilliant. I was trying a regex and it replaced the is in This as well - I had no idea about the word boundaries. Thank you!
1

The replace method doesn’t store the altered value in the same variable. You need to store it in another variable and print it.

tm = tm.replace('\nis \n', '\nis not\n')

Comments

0

you could try splitting up the string, replacing the word and joining the array back together.

tm_array = tm.split("\n")
tm_array[1] = "is not"
new_tm = '\n'.join(tm_array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.