Q:

Remove special symbols / punctuation from a string using python programming

belongs to collection: Python String Exercises

-1

Remove special symbols / punctuation from a string

Given:

str1 = "/*Jon is @developer & musician"

Expected Output:

"Jon is developer musician"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use string functions translate() and maketrans()

  • Use the translate() function to get a new string where specified characters are replaced with the character described in a dictionary or a mapping table.
  • Use the maketrans() function to create a mapping table.

Or Use the regex in Python. See Python regex replace.

Solution1:Use string functions translate() and maketrans().

Use string functions translate() and maketrans().

import string

str1 = "/*Jon is @developer & musician"
print("Original string is ", str1)

new_str = str1.translate(str.maketrans('', '', string.punctuation))

print("New string is ", new_str)

Solution2:Using regex replace pattern in a string

import re

str1 = "/*Jon is @developer & musician"
print("Original string is ", str1)

# replace special symbols with ''
res = re.sub(r'[^\w\s]', '', str1)
print("New string is ", res)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Removal all characters from a string except intege... >>
<< Remove empty strings from a list of strings using ...