Replace all non alphabetic characters python

I have a string with which i want to replace any character that isn't a standard character or number such as (a-z or 0-9) with an asterisk. For example, "h^&ell`.,|o w]{+orld" is replaced with "h*ell*o*w*orld". Note that multiple characters such as "^&" get replaced with one asterisk. How would I go about doing this?

nneonneo

165k35 gold badges293 silver badges368 bronze badges

asked Oct 20, 2012 at 5:10

2

Regex to the rescue!

import re

s = re.sub('[^0-9a-zA-Z]+', '*', s)

Example:

>>> re.sub('[^0-9a-zA-Z]+', '*', 'h^&ell`.,|o w]{+orld')
'h*ell*o*w*orld'

answered Oct 20, 2012 at 5:11

nneonneonneonneo

165k35 gold badges293 silver badges368 bronze badges

6

The pythonic way.

print "".join([ c if c.isalnum() else "*" for c in s ])

This doesn't deal with grouping multiple consecutive non-matching characters though, i.e.

"h^&i => "h**i not "h*i" as in the regex solutions.

crizCraig

7,9385 gold badges53 silver badges52 bronze badges

answered Feb 28, 2014 at 13:27

baloanbaloan

6555 silver badges7 bronze badges

Try:

s = filter(str.isalnum, s)

in Python3:

s = ''.join(filter(str.isalnum, s))

Edit: realized that the OP wants to replace non-chars with '*'. My answer does not fit

answered Jan 5, 2015 at 5:15

Replace all non alphabetic characters python

DonDon

16.4k11 gold badges61 silver badges97 bronze badges

0

Use \W which is equivalent to [^a-zA-Z0-9_]. Check the documentation, https://docs.python.org/2/library/re.html

import re
s =  'h^&ell`.,|o w]{+orld'
replaced_string = re.sub(r'\W+', '*', s)
output: 'h*ell*o*w*orld'

update: This solution will exclude underscore as well. If you want only alphabets and numbers to be excluded, then solution by nneonneo is more appropriate.

Replace all non alphabetic characters python

Csaba Toth

9,2565 gold badges71 silver badges112 bronze badges

answered Aug 12, 2016 at 18:54

psunpsun

5659 silver badges13 bronze badges

2

  1. HowTo
  2. Python How-To's
  3. Remove Non-Alphanumeric Characters From Python String

Created: May-28, 2021

  1. Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String
  2. Use the filter() Function to Remove All Non-Alphanumeric Characters in Python String
  3. Use Regular Expressions to Remove All Non-Alphanumeric Characters in Python String

Alphanumeric characters contain the blend of the 26 characters of the letter set and the numbers 0 to 9. Non-alphanumeric characters include characters that are not letters or digits, like + and @.

In this tutorial, we will discuss how to remove non-alphanumeric characters from a string in Python.

Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String

We can use the isalnum() method to check whether a given character or string is alphanumeric or not. We can compare each character individually from a string, and if it is alphanumeric, then we combine it using the join() function.

For example,

string_value = "alphanumeric@123__"
s = ''.join(ch for ch in string_value if ch.isalnum())
print(s)

Output:

alphanumeric123

Use the filter() Function to Remove All Non-Alphanumeric Characters in Python String

The filter() function is used to construct an iterator from components of the iterable object and filters the object’s elements using a function.

For our problem, the string is our object, and we will use the isalnum() function, which checks whether a given string contains alphanumeric characters or not by checking each character. The join() function combines all the characters to return a string.

For example,

string_value = "alphanumeric@123__"
s = ''.join(filter(str.isalnum, string_value))
print(s)

Output:

alphanumeric123

This method does not work with Python 3.

Use Regular Expressions to Remove All Non-Alphanumeric Characters in Python String

A regular expression is an exceptional grouping of characters that helps you match different strings or sets of strings, utilizing a specific syntax in a pattern. To use regular expressions, we import the re module.

We can use the sub() function from this module to replace all the string that matches a non-alphanumeric character by an empty character.

For example,

import re
string_value = "alphanumeric@123__"
s=re.sub(r'[\W_]+', '', string_value)
print(s)

Output:

alphanumeric123

Alternatively, we can also use the following pattern.

import re
string_value = "alphanumeric@123__"
s = re.sub(r'[^a-zA-Z0-9]', '', string_value)
print(s)

Output:

alphanumeric123

Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • Replace all non alphabetic characters python

    How do you replace a non

    Use the re. sub() method to replace all non-alphanumeric characters in a string, e.g. new_str = re. sub(r'[^a-zA-Z0-9]', '|', my_str) .

    How do I remove all non alphabetic characters in a string Python?

    Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String. We can use the isalnum() method to check whether a given character or string is alphanumeric or not. We can compare each character individually from a string, and if it is alphanumeric, then we combine it using the join() function.

    How do I remove non alphabetic characters from a string?

    A common solution to remove all non-alphanumeric characters from a String is with regular expressions. The idea is to use the regular expression [^A-Za-z0-9] to retain only alphanumeric characters in the string. You can also use [^\w] regular expression, which is equivalent to [^a-zA-Z_0-9] .

    How do you find a non

    Python String isalnum() Method The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)! #%&? etc.