HomeLinuxHow Python Features Take away Characters From A String

How Python Features Take away Characters From A String


Python is a well-liked and versatile programming language that gives a number of options and functionalities. Python programmers encounter a number of issues whereas manipulating strings, that are sequences/collections of characters. To take away sure characters from a string, both in the beginning, ultimately, or within the center, varied inbuilt features are utilized in Python.

The best way to Take away/Delete Characters From a String?

The next strategies are employed in Python to take away or delete characters from a string:

Methodology 1: Take away String Characters Utilizing the “exchange()” Methodology

The “exchange()” methodology is utilized in Python to exchange the actual characters from the outlined string with one other explicit character. This methodology will also be utilized to eradicate a personality from a selected string.

Syntax

string.exchange(oldvalue, newvalue, depend)

Within the above syntax:

  • The “oldvalue” parameter signifies the search string.
  • The “newvalue” parameter specifies the substitute string.
  • The “depend” parameter represents the worth that exhibits what number of occurrences within the string have to be changed.

Instance

The next code is used to take away/delete the characters from the actual Python string:

str1 = “Howdy and Welcome!”
print (“Authentic string: “ + str1)
print (nEradicating all Occurrences:”,str1.exchange(‘o’, ))
print (nEradicating 1st Occurrences:”,str1.exchange(‘o’, , 1))

Within the above code, the “exchange()” methodology takes the string and “prevalence depend” worth as its arguments and deletes the character from the string accordingly. If no depend prevalence worth is given, all of the occurrences of the desired character are faraway from the string.

Output

Methodology 2: Take away String Characters Utilizing the “translate()” Methodology

In Python, the “translate()” methodology is utilized to retrieve a string that comprises explicit characters which were changed with characters described in dictionaries or mapping tables. This methodology will also be employed to take away/delete the character from the Python string.

Syntax

Within the above syntax, the “desk” parameter refers back to the “dictionary” or “mapping desk” indicating the place to exchange it.

Instance

The under code snippet removes the desired character from a string:

str1 = ‘Howdy and Welcome!’
print(‘Given String: ‘, str1)
print(‘After Eradicating Character:’, str1.translate({ord(i): None for i in ‘o’}))

Within the above code, the “translate()” methodology takes the desired dictionary as a parameter and returns the string by eradicating the desired character from the enter string.

Output

Methodology 3: Take away String Characters Utilizing the “re.sub()” Operate

The “re.sub()” operate of the “re” module is utilized to exchange each occasion of the sample within the enter string. This will also be employed to take away/delete the characters from a string.

Syntax

re.sub(sample, repl, string, depend=0, flags=0)

Within the above syntax:

  • The “sample” parameter specifies the string/character that’s going to get replaced.
  • The “repl” parameter refers back to the new string that replaces/substitutes the previous string.
  • The “string” signifies the enter string through which the operation is carried out.
  • The “flags” parameter is used to shorten the code.

Instance

This code makes use of the “re.sub()” methodology to take away the character from the string:

import re
str1 = ‘Howdy and Welcome!’
print(‘Given String: ‘, str1)
print(‘After Eradicating Character:’, re.sub(“o”,“”,str1))

Within the above code, the “re.sub()” operate of the “re” module takes the desired character, empty string, and the initialized string as its arguments, respectively, and removes the desired character from the string.

Output

Methodology 4: Take away String Characters Utilizing the “for” loop

The “for” loop will also be utilized in Python to delete/take away the desired character from the string:

Instance

This code block is utilized to eradicate the desired characters from a string by way of iteration:

str1 = “Howdy and Welcome!”
print(‘Given String: ‘, str1)
str2 = “”
for i in vary(0, len(str1)):
    if i != 1:
        str2 = str2 + str1[i]
print(‘After Eradicating Character:’, str2)

In response to the above code:

  • The unique string and empty string are initialized, respectively.
  • The “for” loop iterates alongside the desired vary and removes the acknowledged character from the string based mostly on the index/place worth.

Output

Conclusion

The “exchange()” methodology, “translate()” methodology, “re.sub()” methodology, or the “for” loop is used to take away/delete the characters from the initialized string. This Python tutorial offered varied methods to take away characters from a string utilizing related examples

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments