Home »
Python
Remove trailing new line in Python
By Sapna Deraje Radhakrishna Last updated : December 21, 2024
Python supports inbuilt methods called strip(), lstrip() and rstrip() which works on string variable and removes the trailing newline or spaces as per given argument.
Remove trailing new line using strip()
The strip() method remove characters from both left and right based on the argument. It returns a copy of the string with both leading and trailing characters stripped.
When the combination of characters in the chars argument mismatches the character of the string in the left/right, the method stops removing the leading/trailing characters respectively,
Syntax
string.strip([chars])
Here, the chars are an optional argument. If not provided, all the leading and trailing whitespaces shall be removed.
Example for strip()
# Define the test strings
test_string = " include help is learning source "
# Strip leading and trailing spaces
print(test_string.strip()) # Output: "include help is learning source"
# Strip specified characters (' include') from both ends
print(test_string.strip(' include'))
# Output: "help is learning sour"
# Note: Along with leading spaces and the word 'include',
# the character 'e' is removed from the trailing.
# Strip specified characters (' test') from both ends
print(test_string.strip(' test'))
# Output: "include help is learning sourc"
# Another test case
test_str = "simple string"
# Strip specified characters ('s') from both ends
print(test_str.strip('s'))
# Output: "imple string"
Remove trailing new line using lstrip()
The lstrip() method returns a copy of the string with leading characters removed based on the argument passed. Similar to strip(), all the combinations of characters in the argument are removed from the left of the string, until the first mismatch.
Syntax
string.lstrip([chars])
chars are an optional argument which if not provided, removes all leading whitespaces from the string.
Example of usage of lstrip()
# Define the test string
test_string = ' test string'
# Use lstrip() with no arguments to remove leading spaces
print(test_string.lstrip())
# Output: "test string"
# Use lstrip() with specified characters ('test')
print(test_string.lstrip('test'))
# Output: " test string"
# Explanation: `lstrip('test')` removes 't', 'e', 's', and 't'
# from the start only if they appear in the given order.
# Use lstrip() with specified characters (' test')
print(test_string.lstrip(' test'))
# Output: "ring"
# Explanation: Along with leading spaces, the characters 't', 'e', 's',
# and 't' are removed from the start.
Remove trailing new line using rstrip()
The rstrip() method returns a copy of the string with trailing spaces removed based on the arguments passed. Here trailing is the right based argument. Similar to strip() and lstrip(), all the combinations of characters in the chars argument are removed from the right of the string until the first mismatch.
Syntax
string.rstrip([chars])
Here, the chars are optional argument, which if not provided all the whitespaces are removed from the string.
Example usage of rstrip()
# Define the first test string
test_string = 'sample test string '
# Use rstrip() to remove trailing spaces
print(test_string.rstrip())
# Output: "sample test string"
# Redefine the test string with trailing spaces and a newline
test_string = 'sample test string test\n'
# Print the string as-is (including the newline)
print(test_string)
# Output:
# sample test string test
# Use rstrip() to remove the trailing newline character
print(test_string.rstrip('\n'))
# Output: "sample test string test"