-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".

3442
  • 8,248
  • 2
  • 19
  • 41
Nicholas Wong
  • 11
  • 1
  • 1

6 Answers6

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
Byte Commander
  • 6,506
  • 6
  • 44
  • 71
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

>>>
kvivek
  • 3,321
  • 1
  • 15
  • 17
  • 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. – Byte Commander Apr 18 '16 at 06:58
0

Use

string_here.rstrip('\n')

To remove the newline.

Harry
  • 11,298
  • 1
  • 29
  • 43
0

Try with strip()

your_string.strip("\n")  # removes \n before and after the string
Mani
  • 933
  • 6
  • 15
  • 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. – Byte Commander Apr 18 '16 at 06: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".

The Defalt
  • 69
  • 8
  • 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. – Byte Commander Apr 18 '16 at 06: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.

mypetlion
  • 2,415
  • 5
  • 18
  • 22
Robert
  • 1