Remove all special characters punctuation and spaces from string

0 votes
I need to remove all special characters, punctuation and spaces from a string so that I only have letters and numbers.
Feb 14 in Python by surbhi
• 3,810 points
649 views

1 answer to this question.

0 votes
This can be done without regex:

>>> string = "Special $#! characters   spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'

You can use str.isalnum:

S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
answered Feb 14 by CoolCoder
• 4,360 points

Related Questions In Python

0 votes
1 answer

How to remove blank spaces from Python string?

You can use the strip method to do ...READ MORE

answered Apr 4, 2019 in Python by Rishi
565 views
0 votes
1 answer

How to remove the white spaces from the string “aaa bbb ccc ddd eee”?

Hi, @Roshni, You can do it Using join, read ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,890 points
867 views
0 votes
1 answer

How to Remove specific characters from a string in Python?

Strings in Python are immutable (can't be changed). Because ...READ MORE

answered Jan 5, 2021 in Python by Gitika
• 65,890 points
311 views
0 votes
1 answer

Extract all characters of a string

Convert it to a list -  s = ...READ MORE

answered Jun 1, 2018 in Python by Nietzsche's daemon
• 4,260 points
305 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,146 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
2,348 views
0 votes
1 answer

Reverse a string in Python

>>> 'hello world'[::-1] 'dlrow olleh' This is extended slice syntax. It ...READ MORE

answered Feb 15 in Python by CoolCoder
• 4,360 points
173 views
0 votes
1 answer

How can I capitalize the first letter of each word in a string?

The .title() method of a string (either ASCII or ...READ MORE

answered Feb 18 in Python by CoolCoder
• 4,360 points
116 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP