Code Coach Python Symbols | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Code Coach Python Symbols

Is there a simpler way of removing all symbols from a string with regular expressions? import re input = input() output = "" pattern = re.compile(r"\w|\s") for i in input: if pattern.findall(i): output += i print(output)

18th Jan 2020, 11:32 PM
Fishbert2000
Fishbert2000 - avatar
10 Answers
+ 6
yes, it can be done in one line if you dont count the import line. check out my code buts in my profile. import re #one liner is shorter print("".join(re.findall(r"[A-Za-z\d\s]+", input())))
10th Feb 2020, 8:55 PM
Ashleigh Fielders
Ashleigh Fielders - avatar
+ 3
Thanks! I didn’t quite get it working that way but it led me to this: import re input = input() output = re.sub(r"[^\w\s]", "", input) print(output)
18th Jan 2020, 11:57 PM
Fishbert2000
Fishbert2000 - avatar
+ 3
Here without regex print(''.join(e for e in input() if e.isalnum() or e==' '))
11th Jun 2021, 11:27 AM
Naveen Surya
Naveen Surya - avatar
+ 1
Here's how I did mine. import re print(''.join(re.findall(r'\w|\s', input())))
23rd Jan 2020, 12:12 AM
Onuoha_ifeanyi
Onuoha_ifeanyi - avatar
+ 1
def clean_string(string): new_string = "" for i in string: if i.isalpha() or i.isspace() or i.isdigit() == True: new_string += i return new_string string = input() print(clean_string(string))
20th Jul 2021, 4:54 PM
Przemysław Komański
Przemysław Komański - avatar
+ 1
My solution: y = "qwertzuiopasdfghjklyxcvbnm QWERTZUIOPASDFGHJKLYXCVBNM1234567890" x = input() output = "" for i in x: if i in y: output += i else: continue print(output)
25th Apr 2022, 7:44 AM
RgHurn
RgHurn - avatar
0
Yes. You can change it to: pattern = re.compile(r"\W|\S") #anything that isn't a letter, underscore, or space Then, you should use re.sub: output = re.sub("", pattern) #removes anything that is a symbol
18th Jan 2020, 11:38 PM
Jianmin Chen
Jianmin Chen - avatar
0
def decode(string): return "".join( c for c in string if c.isalpha() or c.isdigit () or c== " " ) print(decode(input())) try it i use is alpha merthod
20th Jan 2020, 6:08 PM
Sultan
0
import string symbols = string.punctuation scrambled = input() table = str.maketrans("", "", symbols) stripped = scrambled.translate(table) print(stripped)
22nd Feb 2023, 7:24 PM
Chuks AJ
Chuks AJ - avatar
0
My solution with python mess = input() message ="" alphabet=" 1234567890abcdefghijklmnopqrstuvwxyz" for i in mess : for j in alphabet : if i == j or i == j.upper (): message += i print(message)
5th May 2023, 11:36 AM
Oussama Kadimallah
Oussama Kadimallah - avatar