-1

I want to remove \n from a string if it is in a string. I have tried:

slashn = str(chr(92))+"n"
if slashn in newString:
        newerString = newString.replace(slashn,'')
        print(newerString)
else:
    print(newString) 

Assume that newString is a word that has \n at the end of it. E.g. text\n.

I have also tried the same code except slash equals to "\\"+"n".

5
  • This is almost certainly Python, retagging.
    – 3442
    Apr 18, 2016 at 6:28
  • 1
    Are you looking for a newline or a backslash + n?
    – Klaus D.
    Apr 18, 2016 at 6:31
  • 5
    Are you attempting to remove a newline character (\n) or the slash plus letter N sequence (\\n)? Do you clearly understand the difference between both?
    – 3442
    Apr 18, 2016 at 6:31
  • newString.remove('\n')
    – Peter Wood
    Apr 18, 2016 at 6:33

6 Answers 6

2

Use str.replace() but with raw string literals:

newString = r"new\nline"
newerString = newString.replace(r"\n", "")

If you put a r right before the quotes enclosing a string literal, it becomes a raw string literal that does not treat any backslash characters as special escape sequences.

Example to clarify raw string literals (output is behind the #> comments):

# Normal string literal: single backslash escapes the 'n' and makes it a new-line character.
print("new\nline")  
#> new
#> line

# Normal string literal: first backslash escapes the second backslash and makes it a 
# literal backslash. The 'n' won't be escaped and stays a literal 'n'.
print("new\\nline")
#> new\nline

# Raw string literal: All characters are taken literally, the backslash does not have any
# special meaning and therefore does not escape anything.
print(r"new\nline")
#> new\nline

# Raw string literal: All characters are taken literally, no backslash has any
# special meaning and therefore they do not escape anything.
print(r"new\\nline")
#> new\\nline
1

You can use strip() of a string. Or strip('\n'). strip is a builtin function of a string. Example:

>>>
>>>
>>> """vivek
...
... """
'vivek\n\n'
>>>
>>> """vivek
...
... """.strip()
'vivek'
>>>
>>> """vivek
...
... \n"""
'vivek\n\n\n'
>>>
>>>
>>> """vivek
...
... \n""".strip()
'vivek'
>>>

Look for the help command for a string builtin function strip like this:

>>>
>>> help(''.strip)
Help on built-in function strip:

strip(...)
    S.strip([chars]) -> string or unicode

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    If chars is unicode, S will be converted to unicode before stripping

>>>
1
  • Looks like the OP does not want the newline character but a literal \n (backslash character plus n character) to be removed from the string. Apr 18, 2016 at 6:58
0

Use

string_here.rstrip('\n')

To remove the newline.

2
  • I see. I needed to add the character to be stripped.
    – Harry
    Apr 18, 2016 at 6:35
  • Looks like the OP does not want the newline character but a literal \n (backslash character plus n character) to be removed from the string. Apr 18, 2016 at 6:58
0

Try with strip()

your_string.strip("\n")  # removes \n before and after the string
1
  • Looks like the OP does not want the newline character but a literal \n (backslash character plus n character) to be removed from the string. Apr 18, 2016 at 6:58
0

If you want to remove the newline from the ends of a string, I'd use .strip(). If no arguments are given then it will remove whitespace characters, this includes newlines (\n).

Using .strip():

if newString[-1:-2:-1] == '\n': #Test if last two characters are "\n"
    newerString = newString.strip()
    print(newerString)
else:
    print(newString)

Another .strip() example (Using Python 2.7.9)

Also, the newline character can simply be represented as "\n".

1
  • Looks like the OP does not want the newline character but a literal \n (backslash character plus n character) to be removed from the string. Apr 18, 2016 at 6:58
-1
Text="test.\nNext line."
print(Text)

Output:::: test.\nNextline"

This is because the element is stored in double inverted commas.In such cases next line will behave as text enclose in string.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.