Symbols

wordcloud

Note: This page contains a small but interesting piece of Python code which I call snippets. You can find more such codes on my Python snippets page.

Problem

There is a problem with your keyboard: it randomly writes symbols when you are typing a text. You need to clean up the text by removing all symbols.

Task:
Take a text that includes some random symbols and translate it into a text that has none of them. The resulting text should only include letters and numbers.

Input Format:
A string with random symbols.

Output Format:
A string of the text with all the symbols removed.

Sample Input:

l\$e%ts go @an#d@@ g***et #l#unch$$$

Sample Output:
lets go and get lunch

Solution

Here is my solution to the above problem. Remember that there could be more than one way to solve a problem. If you have a more efficient or concise solution, please leave a comment.

In [1]:
import re
print("".join(re.findall('[\w\s]',input())))
lets go and get lunch

Explanation

My approach (or the algorithm)

  • Use RegEx to find alphanumeric characters and spaces.
  • Join them into a string.

The code

  • '[\w\s]' RegEx pattern. \w searches for alphanumeric characters. \s searches for spaces.
  • "".join() joins all the matches into a string.

The problem question is picked from SoloLearn. Here is my SoloLearn code and my SoloLearn profile page.

Last updated 2021-01-09 22:25:05.764668 IST

Comments