1

I have a string as

a='''123
456
789'''

I want to replace 456 with something similar to a null value, so that the corresponding output is

123
789

The objective is to eliminate the empty line that would be generated as a result of

a=a.replace('456','')
3
  • An empty string is different than the value of null, which python does not have Commented Nov 4, 2016 at 5:39
  • 1
    Try the newline character? ('\n456','') Commented Nov 4, 2016 at 5:40
  • yes that works successfully Commented Nov 4, 2016 at 5:42

1 Answer 1

4

You are getting empty line after doing replace because '456' is followed by '\n'. In order to remove empty line, replace '456\n' with '' as:

>>> a = a.replace('456\n', '')
>>> print(a)
123
789

where initially a is holding the multiline string mentioned in the question as:

>>> a='''123
    456
    789'''
Sign up to request clarification or add additional context in comments.

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.