Python | Replace punctuations with K
Sometimes, while working with Python Strings, we have a problem in which we need to perform the replacement of punctuations in String with specific characters. This can have applications in many domains such as day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1: Using string.punctuation + replace() The combination of the above functions can be used to solve this problem. In this, we extract all punctuations using punctuation and perform a replacement of the character desired using replace().
Python3
# Python3 code to demonstrate working of # Replace punctuations with K # Using string.punctuation + replace() from string import punctuation # initializing string test_str = 'w3wiki, is : best for ; Beginner !!' # printing original string print ( "The original string is : " + str (test_str)) # initializing replace character repl_char = '*' # Replace punctuations with K # Using string.punctuation + replace() for chr in punctuation: test_str = test_str.replace( chr , repl_char) # printing result print ( "The strings after replacement : " + test_str) |
The original string is : w3wiki, is : best for ; Beginner!! The strings after replacement : w3wiki* is * best for * Beginner**
Method #2: Using regex This problem can be solved using regex. In this, we employ a regex to substitute for all punctuations.
Python3
# Python3 code to demonstrate working of # Replace punctuations with K # Using regex import re # initializing string test_str = 'w3wiki, is : best for ; Beginner !!' # printing original string print ( "The original string is : " + str (test_str)) # initializing replace character repl_char = '*' # Replace punctuations with K # Using regex res = re.sub(r '[^\w\s]' , repl_char, test_str) # printing result print ( "The strings after replacement : " + res) |
The original string is : w3wiki, is : best for ; Beginner!! The strings after replacement : w3wiki* is * best for * Beginner**
Time Complexity: O(N)
Space Complexity: O(N)
Method #3: Using translate() method replace punctuations with K:
- Initialize the input string “est_str.
- Initialize the replace character repl_char.
- Create a translator using str.maketrans() method with two arguments the first argument is string.punctuation(), which is the list of all punctuations in the string module and The second argument is the “*” character multiplied by the length of the first argument, which will be the replacement character.
- Use the translate() method to replace all the punctuation with the replacement character.
- Store the translated string in “test_str” and print the string after replacement.
Below is the implementation of the above approach:
Python3
# Python program for the above approach import string # initializing string test_str = 'w3wiki, is : best for ; Beginner !!' # printing original string print ( "The original string is : " + str (test_str)) # initializing replace character repl_char = '*' # Replace punctuations with K # Using translate() method translator = str .maketrans( string.punctuation, repl_char * len (string.punctuation)) test_str = test_str.translate(translator) # printing result print ( "The strings after replacement : " + test_str) |
The original string is : w3wiki, is : best for ; Beginner !! The strings after replacement : w3wiki* is * best for * Beginner **
Time Complexity: O(N)
Space Complexity: O(N)
Method #4: using the join() method:
- The string test_str is initialized
- The replace character repl_char is initialized
- The string of all possible punctuation marks is initialized using the punctuation module of the string library.
- The join() method is used to replace each punctuation mark in the string test_str with the replacement character.
- The join() method is used in combination with a ternary operator which checks if each character in test_str is a punctuation mark or not.
- If it is a punctuation mark, then the replacement character is used, otherwise the original character is used. The result of this operation is a string with all the punctuation marks replaced with the replacement character.
- The resulting string is printed using print() function.
Python3
# initializing string test_str = 'w3wiki, is : best for ; Beginner !!' # printing original string print ( "The original string is : " + str (test_str)) # initializing replace character repl_char = '*' # Replace punctuations with K # Using join() method punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' res = ''.join(repl_char if char in punctuations else char for char in test_str) # printing result print ( "The string after replacement : " + res) |
The original string is : w3wiki, is : best for ; Beginner !! The string after replacement : w3wiki* is * best for * Beginner **
The time complexity of this approach is O(n), where n is the length of the input string.
The space complexity of this approach is O(n), where n is the length of the input string
Method 5: Using a loop to iterate through each character in the string and checking if it is a punctuation character.
Step-by-step approach
- Initialize the replace character repl_char to ‘*’.
- Initialize the string of punctuation characters punctuations to ”’!()-[]{};:'”,<>./?@#$%^&*_~”’.
- Initialize an empty string res to store the modified string.
- Use a loop to iterate through each character in the input string test_str.
- Check if the current character is a punctuation character by using the in operator to check if it exists in the punctuations string.
- If the current character is a punctuation character, then append the repl_char to the res string.
- If the current character is not a punctuation character, then append the current character to the res string.
- After iterating through all the characters in the input string, print the modified string using the print() function.
Python3
# initializing string test_str = 'w3wiki, is : best for ; Beginner !!' # printing original string print ( "The original string is : " + str (test_str)) # initializing replace character repl_char = '*' # Replace punctuations with K # Using loop to iterate through each character punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' res = '' for char in test_str: if char in punctuations: res + = repl_char else : res + = char # printing result print ( "The string after replacement : " + res) |
The original string is : w3wiki, is : best for ; Beginner !! The string after replacement : w3wiki* is * best for * Beginner **
Time complexity: O(n) where n is the length of the input string since we iterate through each character in the string once.
Auxiliary space: O(n) since we use a new string to store the modified string which can be of the same length as the input string if all characters are punctuation characters.
Method 6: Using recursive implementation:
- Define a string test_str.
- Initialize the replace character repl_char to *.
- Define a string of punctuations punctuations.
- Initialize an empty string res.
- Iterate over each character in the test_str.
- If the character is a punctuation mark, append repl_char to res, otherwise append the character to res.
- Print the original string and the modified string.
Python3
def replace_punctuations(string, repl_char): punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' if len (string) = = 0 : return '' elif string[ 0 ] in punctuations: return repl_char + replace_punctuations(string[ 1 :], repl_char) else : return string[ 0 ] + replace_punctuations(string[ 1 :], repl_char) # example usage test_str = 'w3wiki, is : best for ; Beginner !!' repl_char = '*' # printing original string print ( "The original string is : " + str (test_str)) res = replace_punctuations(test_str, repl_char) print ( "The string after replacement : " + res) #This code is contributed by Pushpa. |
The original string is : w3wiki, is : best for ; Beginner !! The string after replacement : w3wiki* is * best for * Beginner **
The time complexity: O(n), where n is the length of the input string test_str. This is because we iterate over each character in the string exactly once, and the time taken to perform the other operations is constant.
The space complexity: O(n), because we are creating a new string res of the same length as the input string to store the modified string.