
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace Punctuations in Python with K
In this article we will learn about replacement of punctuation with letter k in the string. You may have seen this problem while performing text processing or data cleaning task in the python where you need to replace punctuations with specific characters or any symbol. Here we will see various methods for replacing the punctuations with given text "k"
Let's get to know this using below example
string = "Welcome, to, * the website ! aliens"
Here we have a string which contains punctuations like [, * !] and we want to replace that with text "k". So finally the string after replacement will be
string = "WelcomeK toK K the website K aliens"
Methods to perform this operation.
Method 1. Using String Replace() Method.
Example
string = "Welcome, to, * the website ! aliens " print("String with punctuation: ", string) modified_str = string.replace(",", "K").replace(".", "K").replace("!", "K").replace("*", "K") print("String after replacing punctuation: ",modified_str)
Output
String with punctuation: Welcome, to, * the website ! aliens String after replacing punctuation: WelcomeK toK K the website K aliens
Explanation
Here in the above example we used replace() function of string which replaces each punctuation with the letter "k". The replace() function will replace all occurrences of the specified punctuation with the given char "k". You can observe here we specified the punctuation into the replace function and if it will match with the char string then it will get replaced.
Method 2. Using Regular Expressions.
Example
import re string = "Welcome, to, * the website ! aliens " print("String with punctuation: ", string) modified_str = re.sub(r"[^\w\s]", "K", string) print("String after replacing punctuation: ",modified_str)
Output
String with punctuation: Welcome, to, * the website ! aliens String after replacing punctuation: WelcomeK toK K the website K aliens
Explanation
Here in the above example we used the re.sub() function from regular expressions to replace punctuation with the given text "k". We used the regular expression pattern [^\w\s] to match string characters which are not words and if we find this, then we will replace the character with "k".
Method 3. Using List Comprehension and Join() Method.
Example
import string string_text = "Welcome, to, * the website ! aliens" print("String with punctuation: ", string_text) modified_str =''.join(["K" if char in string.punctuation else char for char in string_text]) print("String after replacing punctuation: ",modified_str)
Output
String with punctuation: Welcome, to, * the website ! aliens String after replacing punctuation: WelcomeK toK K the website K aliens
Explanation
Here in the above example we used the list comprehension approach for traversing on every character of the text and if we find punctuation then we replaced the punctuation with "k". When we don't find any punctuation then we will leave the char unchanged. Using the join() method we combined the characters.
Method 4. Using Regular Expressions With Character Classes.
Example
import re string_text = "Welcome, to, * the website ! aliens" print("String with punctuation: ", string_text) modified_str=re.sub(r"[.,!*]", "K", string_text) print("String after replacing punctuation: ",modified_str)
Output
String with punctuation: Welcome, to, * the website ! aliens String after replacing punctuation: WelcomeK toK K the website K aliens
Explanation
Here in the above example we used the re.sub() function from regular expressions to replace the punctuation with the character "k". We specified the punctuation character in the regular expressions which is [.,!*]. This punctuation pattern matches the character from the string and replaces the punctuation with "k". You can observe here we specified the punctuation into the expression and if it will match with the char string then it will get replaced.
Method 5. Using List Comprehension With Replace() Function.
Example
import re string_text = "Welcome, to, * the website ! aliens" print("String with punctuation: ", string_text) modified_str = ''.join(["K" if char in [",", ".", "!", "*"] else char for char in string_text]) print("String after replacing punctuation: ",modified_str)
Output
String with punctuation: Welcome, to, * the website ! aliens String after replacing punctuation: WelcomeK toK K the website K aliens
Explanation
Here in the above example, we used List comprehension for traversing on each character of the string text and if we find punctuation then we replace it with the letter "k". In case punctuation doesn't match then it will not replace it. We defined the punctuation using [",", ".", "!", "*"], if you want to add more punctuation characters then you can add them into this list. We used join*() method to combine the characters. You can see in the string_text punctuation is there like "," "*" "!" "." and after performing the operation it is replaced by "k".
So, we get to know about methods to replace the punctuation with "k". We saw various approach to solve the problem which include regular expressions, list comprehension, string methods. We added custom punctuations also and you can also add if you want any other punctuation to be replaced. Each method has its unique approach to solve the problem, you can choose any method which is suitable and easy according to your need.