AppDividend
Latest Code Tutorials

How To Remove Character From String In Python 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

Content Overview

We have two ways to remove the character in Python.

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

#Python remove character from String using 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.

#Python remove character from String using 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 from a 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 the 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.

#Python 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

#Python remove character 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

#Python remove character 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 Example is over.

Recommended Posts

Python In Operator

Python String isupper islower upper lower Functions

Python String startswith Example

Python String Strip Example

Python String Contains Example

Leave A Reply

Your email address will not be published.

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