Python Tutorial - Remove Specified Characters

Python Exercise Lists

3 ways to remove spaces from a string in Python

When the user enters data, it is likely that he or she may inadvertently enter extra spaces, or in some scenarios, spaces and special characters are not allowed before and after the character string. At this time, spaces and special characters in the string need to be removed.

The special characters here refer to the:

  • tab (\t)
  • carriage return (\r)
  • line feed (\n), and so on

In Python, string variables provide three ways to remove extra spaces and special characters in a string. These are:

  • strip(): Removes spaces or special characters before and after (left and right) strings.
  • lstrip(): Removes spaces or special characters in front of (left) the string.
  • rstrip(): Removes spaces or special characters after the string (on the right).

Note that Python's str is immutable (meaning immutable means that once the string is formed, the sequence of characters it contains cannot change in any way), so these three methods just return that the white space before or after the string is removed Subsequent copies do not change the string itself.

Python Strip Function

The strip function is used to remove left and right spaces and special characters. The syntax of this function is:

str.strip([chars])

Among them, str represents the original string, [chars] is used to specify the characters to be deleted, and multiple characters can be specified at the same time. If not specified manually, spaces and special characters such as tabs, carriage returns, and line feeds will be deleted by default.

[Example:]

string = "www.freelearningpoints.com\t\n\r"
string.strip()  #remove special characters by default
string.strip(“,\r”)

The output is:

'www.freelearningpoints.com'
'www.freelearningpoints.com\t\n'

It is not difficult to analyse the results of the analysis. Strip () can indeed remove the spaces and special characters on the left and right sides of the string but does not really change the string itself.

Python lstrip Function

The lstrip () method is used to strip spaces and special characters left side of string. The syntax of the function is as follows:

str.lstrip([chars])

The meaning of the str and chars parameters is exactly the same as str and chars in the strip() syntax, respectively.

[Example:]

string = "www.freelearningpoints.com\t\n\r"
string.lstrip()

The output is:

'www.freelearningpoints.com\t\n\r'

Python rstrip Function

The rstrip() function is used to remove spaces and special characters on the right side of a string. Its syntax is:

str.rstrip([chars])

[Example:]

string = "www.freelearningpoints.com\t\n\r"
string.rstrip()

The output is:

'www.freelearningpoints.com'

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises