AppDividend
Latest Code Tutorials

How To Remove Character From String In Python Tutorial With Example

0

How To Remove Character From String In Python Tutorial With Example is today’s topic. In Python, the string object is immutable and hence sometimes poses visible restrictions while coding the constructs that are required in day-day programming.

This article presents the solution of removing the character from the string. We will see different methods and approaches.

How To Remove Character From String In Python

We have two ways to remove the character in Python.

  1. Using string replace() function.
  2. Using string translate() function.

Using string replace()

We can use the string replace() method to replace the character with a new character. If we provide the empty string as the second argument, then a character will get removed from a string.

One thing to note that the string is immutable in Python so that the replace() function will return the new string and the original string will remain unchanged.

See the following example.

# app.py

strA = "Game of Thrones"
print(strA.replace('Game', ''))

See the output.

Using string replace()

 

In the above output, we have removed the Games from the string. We have removed with an empty string, and now the Games word is removed.

We can replace the word with another word. See the following code.

# app.py

strA = "Game of Thrones"
print(strA.replace('Game', 'Memes'))

See the output.

How To Remove Character From String In Python

 

Now, remove one character from the string. See the following code.

# app.py

strA = "Game of Thrones"
print(strA.replace('G', ''))

See the output.

Ways to remove i’th character from string in Pytho

 

Hence, the string.replace() function can be used for performing the task of removal as we can replace the particular index with empty char, and hence solve the issue.

Using string translate()

Python string translate() function replace each character in a string using the given translation table.

We have to specify a Unicode code point for a character and ‘None’ as the replacement to remove it froma result string. We can use the ord() function to get the Unicode code point of the character.

See the following code example.

Using string translate()

 

If you want to replace the multiple characters, that can be done quickly using an iterator. Let’s see how to remove characters ‘a,’ ‘b’ and ‘c’ from a string. See the below code.

# app.py

strA = "Game of Thrones"
print(strA.translate({ord(i): None for i in 'Game'}))

See the below output.

Using string translate() example

 

You can see in the output that Game word is removed, but also any occurrences of G, a, m, and e character is also removed from the entire string so if the above character is repeated multiple times in the string than also it will remove all the repeated characters.

Removing Spaces from a String

If you want to remove the spaces from the beginning and ending of the string, then you can use the Python String strip standard method.

You can also use the following method. See the below code.

# app.py

strA = " G a m e o f T h r o n e s "
print(strA.replace(' ', ''))
print(strA.translate({ord(i): None for i in ' '}))

See the output below.

Removing Spaces from a String

 

Remove specified number of times

We can also pass a third parameter in replace() function to specify the number of times replacement should be performed. See the following code.

# app.py

strA = "Game of Thrones"
print(strA.replace('e', 'E', 2))

See the output.

Remove specified number of times

 

Python Remove newline from String

See the following code example where we remove /n character.

# app.py

strA = "Game \nof \nThrones"
print(strA.replace('\n', ''))
print(strA.translate({ord('\n'): None}))

See the output.

Python Remove newline from String

 

Using slice + concatenation

One can use string slice and slice the string before the pos i, and slice after the pos i. Then using string concatenation of both, an i’th character can appear to be deleted from the string. See the below code example.

# app.py

strA = "Game of Thrones"
 
print ("The original string is : " + strA) 

# using slice + concatenation 
strO = strA[:2] +  strA[3:] 
  
# Printing string after removal   
# removes ele. at 3rd index 
print ("The string after removal of i'th character : " + strO)

In the above code, we have removed the third index and then concat the remaining string. See the following output.

Using slice + concatenation

 

Finally, How To Remove Character From String In Python Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.