---
---Oct. 5, 2020, 8:11 a.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.

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

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

s="Hello$ Python3$"
s1=s.replace("$", "", 1)
print (s1)
# Результат: 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.

s="Hello$@& Python3$"
import re
s1=re.sub("[$|@|&]","",s)
print (s1)
# Результат: 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

s="Hello$@ Python3&"
s1="".join(c for c in s if c.isalpha())
print (s1) 
# Результат: 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 ()'

s = "Hello$@ Python3&"
f = filter(str.isalpha, s)
s1 = "".join(f)
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 ()'

s = "Hello$@ Python3$"
import re
s1 = re.sub("[^A-Za-z]", "", s)
print (s1)
# Результат: 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.

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

With 're.sub ()'

s = "Hello$@ Python3&_"
import re
s1 = re.sub("[^A-Za-z0-9]", "", s)
print(s1)
# Результат: 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 ()'

s = "Hello347 Python3$"
import re
s1 = re.sub("[0-9]", "", s)
print(s1)
# Результат: 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.

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

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

With 're.sub ()'

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

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

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

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

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

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

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

KR
  • March 28, 2022, 7:14 a.m.

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

Comments

Only authorized users can post comments.
Please, Log in or Sign up
e
  • ehot
  • March 31, 2024, 2:29 p.m.

C++ - Тест 003. Условия и циклы

  • Result:78points,
  • Rating points2
B

C++ - Test 002. Constants

  • Result:16points,
  • Rating points-10
B

C++ - Test 001. The first program and data types

  • Result:46points,
  • Rating points-6
Last comments
k
kmssrFeb. 8, 2024, 6:43 p.m.
Qt Linux - Lesson 001. Autorun Qt application under Linux как сделать автозапуск для флэтпака, который не даёт создавать файлы в ~/.config - вот это вопрос ))
Qt WinAPI - Lesson 007. Working with ICMP Ping in Qt Без строки #include <QRegularExpressionValidator> в заголовочном файле не работает валидатор.
EVA
EVADec. 25, 2023, 10:30 a.m.
Boost - static linking in CMake project under Windows Ошибка LNK1104 часто возникает, когда компоновщик не может найти или открыть файл библиотеки. В вашем случае, это файл libboost_locale-vc142-mt-gd-x64-1_74.lib из библиотеки Boost для C+…
J
JonnyJoDec. 25, 2023, 8:38 a.m.
Boost - static linking in CMake project under Windows Сделал всё по-как у вас, но выдаёт ошибку [build] LINK : fatal error LNK1104: не удается открыть файл "libboost_locale-vc142-mt-gd-x64-1_74.lib" Хоть убей, не могу понять в чём дел…
G
GvozdikDec. 18, 2023, 9:01 p.m.
Qt/C++ - Lesson 056. Connecting the Boost library in Qt for MinGW and MSVC compilers Для решения твой проблемы добавь в файл .pro строчку "LIBS += -lws2_32" она решит проблему , лично мне помогло.
Now discuss on the forum
a
a_vlasovApril 14, 2024, 6:41 a.m.
Мобильное приложение на C++Qt и бэкенд к нему на Django Rest Framework Евгений, добрый день! Такой вопрос. Верно ли следующее утверждение: Любое Android-приложение, написанное на Java/Kotlin чисто теоретически (пусть и с большими трудностями) можно написать и на C+…
Павел Дорофеев
Павел ДорофеевApril 14, 2024, 2:35 a.m.
QTableWidget с 2 заголовками Вот тут есть кастомный QTableView с многорядностью проект поддерживается, обращайтесь
f
fastrexApril 4, 2024, 4:47 a.m.
Вернуть старое поведение QComboBox, не менять индекс при resetModel Добрый день! У нас много проектов в которых используется QComboBox, в версии 5.5.1, когда модель испускает сигнал resetModel, currentIndex не менялся. В версии 5.15 при resetModel происходит try…
AC
Alexandru CodreanuJan. 19, 2024, 11:57 a.m.
QML Обнулить значения SpinBox Доброго времени суток, не могу разобраться с обнулением значение SpinBox находящего в делегате. import QtQuickimport QtQuick.ControlsWindow { width: 640 height: 480 visible: tr…

Follow us in social networks