---
Oct. 5, 2020, 6:11 p.m.

5 Ways to Remove Unnecessary Characters from a String in Python

Remove specific characters from the string


Let's use ' str.replace '

With the help of * str.replace *, we can change some characters to others. If we just want to remove some characters, then we simply replace them with an empty string. * str.replace () * will apply the replacement to all matches found.

  1. s="Hello$ Python3$"
  2. s1=s.replace("$", "")
  3. print (s1)
  4. # Результат: Hello Python3

If we can specify a limit for the number of matches, so as not to remove all characters.

  1. s="Hello$ Python3$"
  2. s1=s.replace("$", "", 1)
  3. print (s1)
  4. # Результат: Hello Python3$

With ' re.sub '

re. sub (pattern, repl, string, count=0, flags=0)

> Returns the string obtained by replacing the leftmost non-overlapping
> matches a pattern in a string to the value repl. If pattern matches
> not found, an unmodified string is returned
> - From Python documentation

If we want to remove characters, then we simply replace the matches with an empty string.

  1. s="Hello$@& Python3$"
  2. import re
  3. s1=re.sub("[$|@|&]","",s)
  4. print (s1)
  5. # Результат: Hello Python3

s1=re.sub(“[$|@|&]”,””,s)

  • Template to replace → * “[$ | @ | &] ”*
    • [] * is used to define a set
    • $ | @ | & * → will search for $ or @ or &
  • Replace with an empty string
  • If the above characters are replaced found, then they are replaced with an empty string

Remove all characters except letters

With 'isalpha ()'

  • isalpha () * is used to check if a string contains only letters. Returns * True * if it is a letter. We will go through each character in the string and check if it is a letter.

Example

  1. s="Hello$@ Python3&"
  2. s1="".join(c for c in s if c.isalpha())
  3. print (s1)
  4. # Результат: HelloPython

s=”Hello$@ Python3&”

(c for c in s if c.isalpha())

Result → * ['H', 'e', 'l', 'l', 'o', 'P', 'y', 't', 'h', 'o', 'n'] *

Before us is a generator object containing all the letters from the string:
s1=””.join(c for c in s if c.isalpha())

  • ””. Join * will join all characters into one line.

With 'filter ()'

  1. s = "Hello$@ Python3&"
  2. f = filter(str.isalpha, s)
  3. s1 = "".join(f)
  4. print(s1)

f = filter(str.isalpha, s)

The * filter () * function will apply the * str.isalpha * method to each element of the string, and if it gets * true *, then we return the element. Otherwise, skip.

s1 = ””.join(f)

The * filter () * function will return an iterator containing all the letters of the given string, and * join () * will "glue" all the elements together.

With 're.sub ()'

  1. s = "Hello$@ Python3$"
  2. import re
  3. s1 = re.sub("[^A-Za-z]", "", s)
  4. print (s1)
  5. # Результат: HelloPython

Consider * s1 = re.sub (“[^ A-Za-z]”, ””, s) *

    • “[ A-Za-z]” * → Searches for all characters except letters. If you specify * * at the beginning of the set, then all those characters that are NOT specified in the set will match the pattern. (for Russian words use * [^ A-Ya-z] * - ed.)
  • All characters matching the pattern will be replaced with an empty string.
  • All characters except letters will be removed.

Remove all characters except letters and numbers

With 'isalnum ()'

  • isalnum () * is used when we want to define whether a string consists of numbers or letters only.

Let's go through each character in the string to identify the characters we need.

  1. s = "Hello$@ Python3&"
  2. s1 = "".join(c for c in s if c.isalnum())
  3. print(s1)
  4. # Результат: HelloPython3

With 're.sub ()'

  1. s = "Hello$@ Python3&_"
  2. import re
  3. s1 = re.sub("[^A-Za-z0-9]", "", s)
  4. print(s1)
  5. # Результат: HelloPython3

Consider * s1 = re.sub (“[^ A-Za-z0–9]”, ””, s) *

    • “[^ A-Za-z0-9]” * → This pattern will search for all characters except letters and numbers.
  • All found characters will be replaced with an empty string
  • All symbols except letters and numbers are removed.

Remove all numbers from a string using regular expressions

With 're.sub ()'

  1. s = "Hello347 Python3$"
  2. import re
  3. s1 = re.sub("[0-9]", "", s)
  4. print(s1)
  5. # Результат: Hello Python$

Consider * s1 = re.sub (“[0–9]”, ””, s) *

    • [0-9] * - numbers from 0 to 9
    • re.sub (“[0–9]”, ””, s) * - if there are matches, replace with an empty string

Remove all characters from the string except numbers

With 'isdecimal ()'

  • isdecimal () * returns true if all characters in the string are numbers, false otherwise.

  1. s = "1-2$3%4 5a"
  2. s1 = "".join(c for c in s if c.isdecimal())
  3. print(s1)
  4. # Результат: 12345

We go over each character of the string and check whether it is a digit. * "". join () * joins all elements.

With 're.sub ()'

  1. s = "1-2$3%4 5a"
  2. import re
  3. s1 = re.sub("[^0-9]", "", s)
  4. print(s1)
  5. # Результат: 12345

Consider * s1 = re.sub (“[^ 0–9]”, ””, s) *

    • [^ 0-9] * will search for all characters except 0 through 9
    • re.sub (“[^ 0-9]”, ””, s) * all characters except numbers will be replaced with an empty string.

With 'filter ()'

  1. s = "1-2$3%4 5a"
  2. f = filter(str.isdecimal, s)
  3. s1 = "".join(f)
  4. print(s1)
  5. # Результат: 12345

Consider * f = filter (str.isdecimal, s) *

The * filter () * function will execute the * str.isdecimal * method for each character, if it returns true, then it adds it to the generator. The generator is then unpacked into a finished string using the * join () * method.

Note

Strings in Python are immutable objects, so all of the above methods remove characters from the given string and return a new one, they do not change the state of the original string.

By article asked0question(s)

4
D
  • Oct. 25, 2021, 10:45 a.m.

Я конечно понимаю, что это статья с Медиума, но всё равно - очень не хватает сравнения скорости выполнения замен.

D
  • Oct. 25, 2021, 10:45 a.m.

Опечатка в заголовке статьи - 5 СОпсобов

KR
  • March 28, 2022, 5:14 p.m.

помогло, спасибо

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html