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, 2022 in Python by surbhi
• 3,810 points
1,105 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, 2022 by CoolCoder
• 4,400 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
814 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,910 points
1,303 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,910 points
660 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
585 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 4,023 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
3,411 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, 2022 in Python by CoolCoder
• 4,400 points
577 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, 2022 in Python by CoolCoder
• 4,400 points
322 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