×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Remove trailing new line in Python

Last Updated : April 27, 2025

In Python, removing unnecessary spaces or newline characters from strings is a common task. Python provides three built-in string methods — strip(), lstrip(), and rstrip() — that make trimming characters extremely easy.

In this tutorial, we'll learn how to use these methods with syntax, examples, and common use cases.

What are strip(), lstrip(), and rstrip()?

  • strip() — Removes characters from both the beginning and end of a string.
  • lstrip() — Removes characters only from the beginning (left side) of a string.
  • rstrip() — Removes characters only from the end (right side) of a string.

By default, all three methods remove whitespace (spaces, tabs, and newlines). However, you can specify a set of characters to remove.

Remove Trailing and Leading Characters with 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

# 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 Leading Characters with 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

# 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 Characters with 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

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

Python Remove Trailing Newline Exercise

Select the correct option to complete each statement about removing trailing newlines in Python.

  1. The ___ method is commonly used to remove trailing whitespace and newline characters from a string.
  2. To specifically remove only the trailing newline character, you can use ___.
  3. The strip() method removes whitespace from ___ of the string.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.