How to Remove Punctuation from a Python String
There are a lot of cases where you may need to remove punctuation from a string. You may want to remove any punctuation from a string number that a user inserts into your program so that you can convert it into an integer. You may want to remove punctuation from a username.
Python has you covered. There’s a number of different ways you can remove punctuation from a string. In this guide, we’re going to talk about how to remove punctuation from a string using the
join() method and the
Without further ado, let’s get started!
Remove Punctuation Python: All Punctuation
We’re building a payments form for a bank.
This form should ask a user for two pieces of information: the number of the account to which they want to transfer money, and the amount they want to transfer.
Let’s start by collecting this information. We can do this using input() statements:
account_number = input("Enter the account number to which you want to transfer money: ") value = input("Enter the amount you would like to transfer: ")
We want to remove any punctuation from these values. This is because we are going to convert them to floating-point numbers later on. Floating-point numbers cannot contain punctuation marks, aside from a full stop (or a “period”).
We can remove all punctuation from these values using the
translate() method. This method makes a copy of a string with a specific set of values substituted.
To make this work, we’re going to use the string.punctuation method. This method, which is part of the “string” library, gives us a list of all punctuation marks.
Let’s remove all the punctuation from both “account_number” and ‘value”:
import string account_number = input("Enter the account number to which you want to transfer money: ") value = input("Enter the amount you would like to transfer: ") final_account_number = account_number.translate(str.maketrans('', '', string.punctuation) ) final_value = value.translate(str.maketrans('', '', string.punctuation) ) print(final_account_number) print(final_value)
First, we import the “string” library. This gives us access to a method called string.punctuation which returns a list of punctuation marks. We then collect input from the user.
Next, we use the
translate() method on both of our strings. The
translate() method replaces every instance of a punctuation mark with the value “” in our strings. We use the
str.maketrans() method to support the translation.
Let’s run our code and see what happens:
Enter the account number to which you want to transfer money: 22-22-2222 Enter the amount you would like to transfer: 10.50 22222222 1050
We added hyphens to the account number and a full stop to the amount we wanted to transfer. In our output, you can see that these characters have been removed.
Remove Punctuation Python: Single Characters
Do you want to remove only a certain set of punctuation from a string? Python has your back.
You can use the join() method to create a copy of a string without certain values present.
We’ve been tasked to build a form that asks a bank account holder to choose a username for their online account. Question marks, full stops, colons, semi colons, and exclamation marks are not allowed. Other special characters, like an underscore, are allowed.
We can remove these individual pieces of punctuation using the join method:
username = input("Enter a username for your online banking account: ") final_username = "".join(u for u in username if u not in ("?", ".", ";", ":", "!")) print(final_username)
First, we ask a user to choose a username. We store that value in a variable called “username”. Next, we use a
join() statement to create a new string without special characters. The
join() statement starts with an empty string and populates it with all the characters that are not in the list we specify.
join() statement uses a list comprehension to loop through every character in the “username” string. As long as that character is not in our list of special characters, it is added to the new string. Otherwise, the character is filtered out.
Let’s run our code:
Enter a username for your online banking account: henry_peters! henry_peters
Our code removed the exclamation mark from the username. It left the underscore. While an underscore is a piece of punctuation, we did not filter it out in our list comprehension.
There are numerous ways to remove punctuation from a string in Python. To remove all punctuation from a string, you can use the
translate() method. You need to use this method with the string.punctuation method, which returns a list of punctuation to filter out from a string.
To remove certain punctuation characters from a string, you can use a custom list comprehension. Inside your list comprehension you can specify the exact characters that you want to remove.
Now you’re ready to remove punctuation from a Python string like an expert!