On this page, we will Strip Punctuation from a String using Python and learn why we should do it.
The reasons why you may want to remove punctuations from a string are countless. For example, you might want to remove all punctuation from user input to convert that input into an integer.
Contents
Strip Punctuation from a String using Python
Or you may want to remove punctuation from a username perhaps and the list goes on. Whatever the reason, Python has you covered!
In this guide, we will look at 2 of the best ways to successfully remove punctuation from the user input. Without further ado, let us get to it.
Using THE join() METHOD
This method is useful when you want to remove only a certain set of punctuation. Join() method makes a copy of the string without certain values present.
For example:
We have asked the user to enter his username and we wish to make sure that no special characters are present in the final output. This is how we can complete the task:
username="WholeBlog.com!""}{?:""}" final_username= "".join(u for u in username if u not in ("?", ".", ";", ":", "!","}","{")) print(final_username)
Explanation of the program
First, we ask the users to enter their username by prompting a message on the console which reads “Enter username”.
After the input has been given, we create a new variable called “final_username” in there we would store the new string (without the special characters).
Now we use the join() statement to create this new string to be stored in final_username.
The join() statement starts as an empty string and fills it with all the characters that are not part of the list specified.
- In simple words, a for-loop is used to pick out each character from the given string which is then compared to the list of special characters that we have specified.
- If the character is not in the list, it is added to our final string.
- Otherwise, the character is filtered out.
Read more: Ways To Use Python If Not
USING IF-ELSE STATEMENT
punctuations = '''!()-[]{};:'"/,<>./?@#$%^&*_~''' my_str = "Hello world! what's up?:#$@#{_" final_string = my_str for char in final_string: if char not in punctuations: final_string = final_string + char print(final_string)
The above code is just another way of stripping punctuations from our string, although this method is simpler than the one described previously.
In this method, we first initialize a list of characters that we don’t want to do include in the final string.
- Next, we ask the user for an input string, and then we iterate a for-loop through the input.
- In each iteration, an If-statement is used to compare every character from the input string to the list of punctuations we previously initialized.
- If the character is not present in the list, it is added to the final string.
- Otherwise, it is filtered out (similar to the previous example).
Conclusion
There are quite a few ways to strip punctuations from a string but the ones we discussed above are the simplest and efficient especially for people who have just started programming.
Efficiency and compatibility are things to be kept in mind while coding so our program can be modified and edited easily whenever need be.
Happy coding!!
Read more: How to Check if File Exists Using Python?