How to get rid of '\n' at the end of a line ?
Ionuț G. Stan
171k18 gold badges187 silver badges199 bronze badges
asked Jan 30, 2009 at 12:58
3
"string \n".strip[];
or
"string \n".rstrip[];
answered Jan 30, 2009 at 13:00
Ionuț G. StanIonuț G. Stan
171k18 gold badges187 silver badges199 bronze badges
If, as Rolf suggests in his comment, you want to print text without having a newline automatically appended, use
print "foo",
Note the trailing comma.
answered Jan 30, 2009 at 13:10
unwindunwind
383k64 gold badges462 silver badges595 bronze badges
1
Get rid of just the "\n" at the end of the line:
>>> "string \n".rstrip["\n"]
'string '
Get rid of all whitespace at the end of the line:
>>> "string \n".rstrip[]
'string'
Split text by lines, stripping trailing newlines:
>>> "line 1\nline 2 \nline 3\n".splitlines[]
['line 1', 'line 2 ', 'line 3']
answered Jan 30, 2009 at 15:03
Ryan GinstromRyan Ginstrom
13.7k5 gold badges44 silver badges60 bronze badges
In Python 3, to print a string without a newline, set the end to an empty string:
print["some string", end=""]
answered Jan 30, 2009 at 23:59
Winston Chuen-Shih YangWinston Chuen-Shih Yang
If you want a slightly more complex and explicit way of writing output:
import sys
sys.stdout.write["string"]
Then you would responsible for your own newlines.
answered Jan 30, 2009 at 14:25
Gregg LindGregg Lind
20.1k15 gold badges66 silver badges81 bronze badges
Created: May-09, 2021 Strings in Python can be defined as the cluster of Unicode characters enclosed in single or double quotes. Like other popular
programming languages, Python also has a newline character indicated by Newline characters can also be used in f-strings. Moreover, according to the Python documentation, print statements add a newline character at the end of a string by default. This tutorial will discuss different methods to remove a newline character from a string in Python. The The following code uses the Output: The
The following code uses the Output: Also known as the brute force method, It uses the We use a List of strings and implement this method on it. Lists are one of the four built-in datatypes
provided in Python and can be utilized to stock multiple items in a single variable. The following code uses the Output: The The
The The following code uses the Output:strip[]
Function to Remove a Newline Character From the String in Pythonreplace[]
Function to Remove a Newline Character From the String in Pythonre.sub[]
Function to Remove a Newline Character From the String in Python\n
. It is essentially used to trace the culmination of a line and the emergence of a new line in a string.Use the
strip[]
Function to Remove a Newline Character From the String in Pythonstrip[]
function is used to remove both trailing and leading newlines from the string that it is being operated on. It also removes the whitespaces on both sides of the string.strip[]
function to remove a newline character from a string in Python.str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip[]
print[newstr]
Starbucks has the best coffee
rstrip[]
function can be used instead of the strip function if there is only the need to remove trailing newline characters. The leading newline characters are not affected by this function and remain as they are.rstrip[]
function to remove a newline character from a string in Python.str1 = "\n Starbucks has the best coffee \n"
newstr = str1.rstrip[]
print[newstr]
Starbucks has the best coffee
Use the
replace[]
Function to Remove a Newline Character
From the String in Pythonfor
loop and replace[]
function. We look for the newline character \n
as a string inside a string, and manually replace that from each string with the help of the for
loop.replace[]
function to remove a newline character from a string in Python.list1 = ["Starbucks\n", "has the \nbest", "coffee\n\n "]
rez = []
for x in list1:
rez.append[x.replace["\n", ""]]
print["New list : " + str[rez]]
New list : ['Starbucks', 'has the best', 'coffee ']
Use the
re.sub[]
Function to Remove a Newline Character From the String in Pythonre
module needs to import to the python code to use the re.sub[]
functionre
module is a built-in module in Python, which deals with regular expression. It helps in performing the task of searching for a pattern in a given particular string.re.sub[]
function is essentially used to take a substring and replace its occurrence in the string with another substring.re.sub[]
function to remove a newline character from a string in Python.#import the regex library
import re
list1 = ["Starbucks\n", "has the \nbest", "coffee\n\n "]
rez = []
for sub in list1:
rez.append[sub.replace["\n", ""]]
print["New List : " + str[rez]]
New List : ['Starbucks', 'has the best', 'coffee ']
Related Article - Python String