How to Remove/Eliminate Python Newline From String?
To remove a newline from a string, the following approaches are used in Python:
- “strip()” Method.
- “replace()” Method.
- “List Comprehension” With the “join()” Method.
- “re.sub()” Function.
Method 1: Remove Newline From a String Using the “strip()” Method
The “strip()” method is applied to eliminate/remove any leading (spaces at the start) and trailing (extra spaces at the end) characters from a string. By default, it removes the spaces at the beginning of a string. We can utilize this method to terminate a new line from a string.
Syntax
In the above syntax, the “string” refers to the string that you want to strip, and the “characters” are the characters that you want to remove from the string. When no characters are specified, then it will eliminate all the beginning spaces of a string.
Example
In the below code, the “strip()” method is used to remove a newline from a string:
print(string_value)
newstr = string_value.strip()
print(newstr)
In the above code snippet, the “strip()” method removes the leading newline from the specified string since no character is specified.
Output
In the above snippet, the leading new line from the specified string has been removed.
Method 2: Remove Newline From a String Utilizing the “replace()” Method
The “replace()” method is used to replace all the occurrences of the specified substring with the new string and returns the new string. This method can also be applied to terminate a new line from a string.
Syntax
According to the above syntax:
- “oldvalue” is the string to be replaced.
- “newvalue” refers to the string that will replace the old value.
- “count” (optional) indicates how many times to replace the old value.
Note: If you don’t specify a count value, all occurrences of the old value will be replaced with the new value.
Example
In the below code, the newline is removed from the given string:
print(string_value)
newstr = string_value.replace("\n", "")
print(newstr)
In the above code, the “replace()” method takes the newline character “\n” and the empty string as its arguments, respectively, and removes all the newlines.
Output
The above output implies that all the newline characters from the given string have been removed accordingly.
Method 3: Remove Newline From a String Using “List Comprehension” With the “join()” Method
“List Comprehension” is a concise way to create/return a new list by using the existing element of the list. The “join()” method is used to join or combine the string to an empty string. These methods are used in combination to remove a new line from a string.
Syntax
Based on the above syntax, “string” indicates the separator string that will be used to join the elements of the “iterable” object passed as an argument to the method.
The syntax of the “List Comprehension” approach is shown below:
Example
In the following example code, the newline is removed from the given string:
print(string_value)
newstr = "".join([i for i in string_value if i !="\n"])
print(newstr)
Based on the above lines of code, the “List Comprehension” approach is used combined with the “join()” method to remove the newline from the specified string and join the string to an empty string without the newline character.
Output
The above output displays that the newline has been removed from the given string appropriately.
Method 4: Remove Newline From a String Using the “re.sub()” Function
The “re.sub()” function is used to replace single or multiple matches with the string in a given text using regular expression.
Syntax
In the above syntax:
- “pattern” specifies/represents the regular expression pattern to match.
- “repl” refers to the string that you want to replace with.
- “string” corresponds to the string where you want to perform the replacement.
- “count” (optional) parameter is the total number of instances that you intend to replace.
- “flags” (optional) parameters are special flags that modify the behavior of the function.
Example
The following code removes the newline from the given string:
string_value = "\nPython \nGuide\n"
print(string_value)
newstr = re.sub(r'[\n\r]+', '', string_value)
print(newstr)
In the above code block, the “re.sub()” function takes the pattern(matching the newline) and the given string as its arguments, respectively, and removes the newline from the string based on that.
Output
The above outcome shows that the newline has been removed from the input string successfully.
Conclusion
To remove a new line from a string in Python, the “strip()” method, the “replace()” method, “List Comprehension” with the “join()” method or the “re.sub()” function can be used. The “strip()” method removes the leading and trailing newline from the string. Likewise, the other approaches such as the “replace()” method, or the “re.sub()“ function, etc. are used to remove newlines from the given string efficiently. This blog demonstrates how to eliminate a newline from a Python string.