How to remove special characters from String in Python - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here we will see how to remove special characters from String in Python.
Python string package is a very good solution to remove the special characters from a string. In the following example, I have prepared a string having different special characters, lets see how we can remove them using python string package.
import string

if __name__ == '__main__':
    data = '#(Hello! I need to remove $pl @hars in Pyth@n)'
    cleaned_data = data.translate(str.maketrans('','',string.punctuation))
    print(cleaned_data)
Output:
Hello I need to remove pl hars in Pythn
The cstr.maketrans() is a static method returns a translation table that will map each character in from into the character at the same position in to; from and to must be bytes objects and have the same length.
string.punctuation is a pre-initialized string with a set of punctuations
We can remove the special characters using a python regular expression package. As string.punctuation has limited set of punctuations if your string has lot more type of special characters you can go with python regex.
import re
if __name__ == '__main__':
    data = '#(Hello! I need to remove $pl @hars in Pyth@n)'

    cleaned_data = re.sub('[^A-Za-z0-9 ]+', '', data)
    print(cleaned_data)
Output:
Hello I need to remove pl hars in Pythn
Python Built-ins
Python re
Happy Learning 🙂

How to access for loop index in Python
How to Remove Spaces from String in Python
Python List Data Structure In Depth
How to Create or Delete Directories in Python ?
Python raw_input read input from keyboard
Python Tuple Data Structure in Depth
How to get Words Count in Python from a File
How to sort python dictionary by key ?
How to get Characters Count in Python from a File
Java Program to Find the characters count in String
Python – How to remove key from dictionary ?
Python – How to remove duplicate elements from List
How to remove empty lists from a Python List
Python String to int Conversion Example
Python – Print different vowels present in a String

How to access for loop index in Python
How to Remove Spaces from String in Python
Python List Data Structure In Depth
How to Create or Delete Directories in Python ?
Python raw_input read input from keyboard
Python Tuple Data Structure in Depth
How to get Words Count in Python from a File
How to sort python dictionary by key ?
How to get Characters Count in Python from a File
Java Program to Find the characters count in String
Python – How to remove key from dictionary ?
Python – How to remove duplicate elements from List
How to remove empty lists from a Python List
Python String to int Conversion Example
Python – Print different vowels present in a String
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list