Remove character from string Python (Complete tutorial)

In this Python tutorial, we will discuss how to remove character from string Python. We will also check:

  • Python remove a character from string
  • Python remove a character from a string using replace() method
  • Python remove multiple characters from a string using replace() method
  • Python remove a character from a string using translate() method
  • Python remove multiple characters from a string using translate() method
  • Remove first character from string python
  • Remove n character from string python
  • Remove newline from string python
  • Remove specified number of times in python
  • Python replace multiple characters in a string
  • Remove string from string python
  • How to remove punctuation from a string python
  • Remove last character from string python
  • Remove last 4 characters from string python
  • Python remove all whitespace from a string
  • Python remove only leading and trailing spaces
  • Remove multiple characters from a string in python
  • Remove spaces from string python
  • python strip substring from a string
  • Remove a character from a string python by index
  • Remove a character from a string python pandas
  • Python remove a special character from the string
  • Python remove a character from a string in the list
  • Python remove all instances of a character from a string

Python remove a character from a string

Let us see an example of Python remove a character from a string.

Removing characters from a string can be most useful in many applications. Filter texts, sentiments always require the main method and solution of being able to delete a character from a string.

  • You can easily remove a character from a string using replace() and translate() method.
  • To remove a character from a string there are many ways to solve this.
  • we will discuss the following approaches.
    • Using the Python replace() method
    • Using the translate() method
    • Using slicing method
    • Using join() method
    • Using filter() method
  • Replace(): This function is a built-in string method that replaces one character from another and always displays a new string as a result.

Syntax:

Here is the Syntax of replace() method

replace
       [
        old_str
        new_str
        instance
       ]
  • Translate(): the method will change the string by replacing the character. We have to generate the Unicode for the character and None as a replacement value to delete the value from the main string.

Syntax:

Here is the Syntax of translate() method

str.translate(table)

A translation table store the mapping between two characters was create by the maketrans() method.

  • Slicing():This method returns the characters falling between indices a and b. If the user wants to delete the character at a particular index then we use the slicing() method.

Syntax:

String
      [start:end:step_value]

Example:

Let’s take an example to check how to remove a character from a string using the slicing method

str="Japan"
str=str[:3]+str[4:] #remove character at index 3
print(str) 

Here is the Screenshot of the following given code

Python remove character from string using slicing method
Python Remove character from string using slicing method
  • Join(): It is a method that joins each value and character of the iterable object with the string and returns a new string. To delete a character from a string we can easily use the join() method, we will have to iterate the sequence through the string and remove the character.

Syntax:

string_name.join(iterable) 

Example:

Let’s take an example to check how to remove a character from a string using the join method

str="Australia Germany France"
list=['a','r'] 
str2="".join(i for i in str if i not in list) 
print(str2) 

Here is the Screenshot of the following given code

Python remove character from a string using join
Python Remove character from a string using join
  • Filter(): The filter method gives an iterable sequence with the help of a join() function that tests each value and character in the iterable sequence to be true or not.

Example:

Let’s take an example to check how to remove a character from a string using the filter() method

str="Russia England China"
remove_char=['a','i','n'] 
new=filter(lambda i: i not in remove_char,str) 
new_str="" 
for i in new: 
    new_str+=i 
print(new_str) 

Here is the Screenshot of the following given code

Python remove character from string using filter
Python remove a character from a string using a filter

Read: Remove Unicode characters in python

Python remove a character from a string using replace() method

  • In this method, we will learn and discuss how to remove a character from a String using replace() method.
  • This method can be easily used to replace any character with a blank string char.
  • We can use replace() method to remove a character with a new character.
  • Using “” as the new character can be easily used to delete a character from a string.

Syntax:

replace
       [
        old_str
        new_str
        instance
       ]

Example:

Let’s take an example to check how to remove a character from a string using replace() method

str1 = "Germany France"
print(str1.replace('e','o'))
  • In the above code, we will create a variable and assign a string and use the function str.replace().
  • In this example, we will replace the character ‘e’ with ‘o’.
  • If we generate the empty string as the second parameter, then the character will easily get removed from the string.

Here is the Screenshot of the following given code

Python remove character from string using replace
Python remove a character from string using replace

Read: Python remove substring from a String

Python remove multiple characters from a string using replace() method

  • In this method, we will learn and discuss how to remove multiple characters from a String using replace() method.
  • To remove multiple characters from a string we can easily use the function str.replace and pass a parameter multiple characters.
  • The String class (Str) provides a method to replace(old_str, new_str) to replace the sub-strings in a string. It replaces all the elements of the old sub-string with the new sub-string.

Syntax

Here is the Syntax of the replace() method

replace
       [
        old_str
        new_str
        instance
       ]

Example:

Let’s take an example to check how to remove multiple characters from a string using replace method

str = "Germany France"
result = str.replace('a', 'b').replace('e', 'u')
print(result)

Here is the Screenshot of the following given code

Python remove multiple characters from a string using replace
Python remove multiple characters from a string using replace

Python remove a character from a string using translate() method

  • In this method, we will learn and discuss how to remove a character from a String using the translate() method.
  • In the translate() method, we have to generate the Unicode code point for the character and ‘None’ as a replacement to delete it from the result string.
  • In translate() we can easily use ord() function to get the unicode character.

Syntax:

Here is the Syntax of translate() method

str.translate(table)

A translation table storing the mapping between two characters was declared by the maketrans() method.

Example:

Let’s take an example to check how to remove a character from a string using the translate method.

str = "U.S.A southAfrica Australia "

print(str.translate({ord('r'): None}))

Here is the Screenshot of the following given code

Python remove a character from string using translate
Python remove a character from a string using translate

Read: How to convert list to string in Python

Python remove multiple characters from a string using translate() method

  • In this section, we will learn and discuss how to remove multiple characters from a String using the translate() method.
  • If the user wants to replace multiple characters, that can be done easily using a sequence iterator which loops through a string of characters that we want to delete from a string.
  • We are using a list comprehension method to iterate every character.

Syntax:

Here is the Syntax of translate() method

str.translate(table)

Example:

Let’s take an example to check how to remove multiple characters from a string using the translate() method.

str = "Micheal George James"

print(str.translate({ord(i): None for i in 'aeo'}))

Here is the Screenshot of the following given code

Python remove multiple characters from a string using translate
Python remove multiple characters from a string using translate

Remove first character from string Python

Now, we will see how to remove the first character from the string in Python. We can use replace() function for removing the character with an empty string as the second argument, and then the character is removed.

Example:

my_string = 'Welcome'
print(my_string.replace('W', '')

After writing the above code (remove the first character from string python), Ones you will print “ my_string.replace() ” then the output will appear as an “ elcome ”. Here, the first character is ‘W’ which is replaced with an empty string in python.

You can refer to the below screenshot to remove the first character from string python 

Remove character from string Python
Remove the first character from string python

This is how we can remove the first character from string python.

Read: Convert string to float in Python + Various Examples

Remove n character from string python

Now, we will see how to remove n character from string in Python. We can use the string slicing ” [n:] ” where n is used for the amount of character to remove from the string.

Example:

my_string = 'Welcome'
remove = my_string[3:]
print(remove)

After writing the above code (remove n character from string python), Ones you will print “ remove ” then the output will appear as a “ come ”. Here, n is 3 so the first 3 characters are removed from the string python.

You can refer to the below screenshot to remove n character from string python

Remove n character from string python
Remove n character from string python

This is how we can remove n character from string python.

Read: Append to a string Python

Remove newline from string python

In python, to remove newline from string we can use replace() function for removing the ” \n ” from the string and it will remove all newline.

Example:

my_string = 'Welcome\nto\n2020'
print(my_string.replace('\n', ''))

After writing the above code (remove the newline from string python), Ones you will print “ my_string.replace() ” then the output will appear as a “ Welcometo2020 ”. Here, ” \n ” is removed with the empty string as a second argument, and the newline is removed from the string.

You can refer to the below screenshot to remove newline from string python

Remove newline from string python
Python Remove newline from string

This is how we can remove newline from string python

Read: Add string to list Python + Examples

Remove specified number of times in python

In python, to remove a specified number of times we can use replace() function with 3 parameters to specify the number of times replacement should take place in a string.

Example:

my_string = 'Welcome'
print(my_string.replace('e', 'E', 2))

After writing the above code (remove the specified number of times in python), Ones you will print “ my_string.replace() ” then the output will appear as a “ WElcomE ”. Here, ” e ” is removed with ‘ E ‘ as a second argument and the third argument is the number of times replacement takes place.

You can refer to the below screenshot to remove specified number of times in python

Remove specified number of times in python
Remove specified number of times in python

This is how we can remove specified number of times in python.

Python replace multiple characters in a string

In python, to replace multiple characters in a string we will use str.replace() to replace characters and it will create a new string with the replaced characters.

Example:

my_string = "Sixty"
remove = ['t', 'y']
for value in remove:
my_string = my_string.replace(value, '')
print(my_string)

After writing the above code (python replace multiple characters in a string), Ones you will print “ my_string ” then the output will appear as a “ Six ”. Here, ” t ” and ” y ” old value are replaced with new which is an empty string.

You can refer to the below screenshot python replace multiple characters in a string

Python replace multiple characters in a string

This is how we can replace multiple characters in a string.

Read: Python program to reverse a string with examples

Remove string from string python

In python, to remove a string from the string we will use a str.replace() method for removing the string from string python and it will create a new string.

Example:

my_string = "Sixty people arrived in the hostel"
remove = ['arrived']
for value in remove:
my_string = my_string.replace(value, '')
print(my_string)

After writing the above code (remove string from string python), Ones you will print “ my_string ” then the output will appear as a “ Sixty people in the hotel ”. Here, ” arrived ” is removed with the empty string.

You can refer to the below screenshot python remove string from string python

Remove string from string python

This is how we can remove string from string python

How to remove punctuation from a string python

In python, to remove punctuation from a string python we will use for loop to remove all punctuation from the string python.

Example:

punctuation = '''!/[email protected]#$%^&*_~()-[]{};:'"\,<>.'''
my_string = "Hello!? World!!"
remove_punct = ""
for character in my_string:
if character not in punctuation:
remove_punct = remove_punct + character
print(remove_punct)

After writing the above code (how to remove punctuation from a string python), Ones you will print “ remove_punct ” then the output will appear as a “ Hello World ”. Here, we will check each character of the string by using for loop, and it will remove all the punctuation from the string.

You can refer to the below screenshot for how to remove punctuation from a string python

How to remove punctuation from a string python

This is how we can remove punctuation from a string python

Read: Python string formatting with examples

Remove last character from string python

In python, for removing the last character from string python we will use the string slicing technique for removing the last character use negative index “my_string[:-1]” it will remove the last character of the string.

Example:

my_string = 'University'
remove_char = my_string[:-1]
print(remove_char)

After writing the above code (remove the last character from string python), Ones you will print “remove_char ” then the output will appear as a “ Universit ”. Here, we will use negative index -1 to remove the last character from the university.

You can refer to the below screenshot remove the last character from string python

Remove the last character from string python

This is how we can remove the last character from string python

Remove last 4 characters from string python

In python, for removing the last 4 characters from the string python we will use the string slicing technique for removing the last 4 characters by using the negative index “my_string[:-4]” and it will remove the last 4 characters of the string.

Example:

my_string = 'University'
remove_char = my_string[:-4]
print(remove_char)

After writing the above code (remove last 4 characters from string python), Once you will print “remove_char ” then the output will appear as a “ Univer”. Here, we will use a negative index -4 to remove the last 4 characters from the university.

You can refer to the below screenshot remove the last 4 characters from the string python

Remove last 4 characters from string python

This is how we can remove last 4 characters from string python

Read: How to concatenate strings in python

Python remove all whitespace from a string

In python, to remove all whitespace from a string we will use replace() to remove all the whitespace from the string.

Example:

my_string = " Welcome to Python "
remove = my_string.replace(" ", "")
print(remove)

After writing the above code (python remove all whitespace from a string), Ones you will print “remove” then the output will appear as a “ WelcometoPython ”. Here, we will use replace() to remove all the whitespace from the string.

You can refer to the below screenshot python remove all whitespace from a string

Python remove all whitespace from a string

This is how we can remove all whitespace from a string python

Read: How to Convert Python string to a byte array with Examples

Python remove only leading and trailing spaces

In python, to remove only leading and trailing spaces we will use the strip() function to remove the leading and trailing characters from the start and end of the string.

Example:

my_string = " Welcome to Python \n\r\t "
remove = my_string.strip()
print(remove)

After writing the above code (python remove only leading and trailing spaces), Ones you will print “remove” then the output will appear as a “ Welcome to Python ”. Here, we will use the strip() function to remove the leading and trailing characters and whitespaces from the start and end of the string.

You can refer to the below screenshot python remove only leading and trailing spaces.

Python remove only leading and trailing spaces

This is how python remove only leading and trailing spaces.

Remove multiple characters from a string in python

  • To remove multiple characters from a string, we will first create a copy of the original string.
  • Put in one string the multiple characters that will be removed.
  • Then for-loop is used to iterate through each character.
  • Then call new_string.replace() to replace old with new.

Example:

o_string = "([email protected])!"
characters_remove = "()@!"
new_string = o_string
for character in characters_remove:
  new_string = new_string.replace(character, "")
print(new_string)

After writing the above code (remove multiple characters from a string in python), once you will print the “new_string” then the output will appear as “PythonGuides”. Here, the multiple characters from a string will be removed and it will return a new string that will exclude those characters.

You can refer to the below screenshot remove multiple characters from a string in python

Remove multiple characters from a string in python
Remove multiple characters from a string in python

The above code we can use to remove multiple characters from a string in Python.

How to remove spaces from string python

In python, to remove spaces from a string, we have replaced () method to remove all the spaces between the words, it will remove the whitespaces from the string.

Example:

string = ' Welcome to Python '
remove = string.replace(" ", "")
print(remove)

After writing the above Python code ( remove spaces from string python ), Ones you will print “remove” then the output will appear “WelcometoPython”. Here, replace() will remove all the whitespaces from the string. Also, you can refer to the below screenshot.

Remove spaces from string python
Remove spaces from string python

The above code we can use to remove spaces from string python.

Python strip substring from string

Let us see an example of Python strip characters from a string.

The strip() method in python returns a copy of the string. It will strip the specified characters from a string.

Example:

my_str = "www.pythonguides"
r = my_str.strip("guides")
print(r)

After writing the above code (python strip characters from a string), once you will print the “r” then the output will appear as “www. python”. Here, my_str.strip(“guides”) is used to strip the character “guides” from the string.

You can refer to the below screenshot of python strip characters from a string.

Python strip characters from a string
Python strip characters from a string

Python remove a specified character from a string

  • In this section, we will learn how to remove a specified character from a String.
  • Python removes a character from String using multiple methods by which we can easily remove a character from String.
  • Here is the list of methods
    • String replace()
    • String translate()

String replace() method replaces a specified character with another specified character.

Syntax:

Here is the Syntax of String replace()

replace[
        
        old_Str1,
        new_Str2,
        instance
       ]
        

Let’s take an example to check how to remove a character from String

str1 = 'john'
print(str1.replace('o',''))

Here is the screenshot of the following given code

Python remove character from string
Python remove a character from string

String translate(): It will change the string by replacing the character or by deleting the character. We have to specify the Unicode for the character as a replacement to remove it from the String.

Let’s take an example to check how to remove a character from String by using translate()

str1 = 'john'
print(str1.translate({ord('o'):None}))

Here is the screenshot of the following given code.

Python remove character from string translate method
Python remove a character from string translate method

Remove a character from a string Python by index

  • In this section, we will learn how to remove a character from a String Python by index.
  • Remove a character from a string by index we can easily use string by slicing function.

The string Slicing() method always returns the characters falling between indices a and b.Starting at a, a+1,a+2……till b-1.

Syntax:

Here is the Syntax of String Slicing.

String
      [start:end:step_value]

Let’s take an example to check how to remove a character from a String Python by index.

str1 = "William"
print(str1[3:-2])

Here is the screenshot of the following given code.

Remove a character from a string python by index
Remove a character from a string python by index

Remove a character from a string python pandas

Pandas is a python module that is used for data manipulation analysis and cleaning. Python pandas modules or libraries are well-suited for different sets of data such as we can work on tabular data or series.

  • In this method, we will learn and discuss how to remove a character from a String Python pandas.
  • First, we have to create a Data Frame with one Column that stores a String.
  • Then we have to use the str replace() method which specified a character with another specified character.

Syntax:

Here is the Syntax of String Slicing.

Column name
           [
            replace[
        
        old_Str1,
        new_Str2,
        instance
       ]
]

Let’s take an example to check how to remove a character from String Python Pandas.

import pandas as pd

dt = {'ALPHABET':  ['a','z','f','h']
          
         }
df = pd.DataFrame(dt, columns= ['ALPHABET'])
df['ALPHABET'] = df['ALPHABET'].str.replace('a','l')
print (df)

Here is the screenshot of the following given code.

Remove character from string python pandas
Remove character from string python pandas

Python remove a special character from a string

  • In this section, we will learn how to remove a special character from a String in Python.
  • Python removes a special character from String using multiple methods by which we can easily remove a character from String.
  • Here is the list of methods
    • Str. isalnum()
    • filter(str.isalnum, Str2)

The Str. isalnum() method always returns a boolean which means all special characters will remove from the string and print the result true. It will always return False if there is a special character in the string.

Let’s take an example to check how to remove a special character from the string using the str. isalnum() method.

str1 = "John! what's up#"
new_str2 = ''.join(char for char in str1 if char.isalnum())
print(new_str2)

Here is the screenshot of the following given code

remove a special character from string in python
Remove a special character from string in python

The filter(str. isalnum, Str2) function is also used for deleting a special character. But in this function we are not using for loop and if statement on str. isalnum, Str2. We will use the filter() method.

Let’s take an example to check how to remove a special character from the string using the filter(str. isalnum, Str2)

str2 = "John! what's up#"
new_str1 = ''.join(filter(str.isalnum,str2))
print(new_str1)

Here is the screenshot of the following given code

Remove a special character from string in python using filter
Remove a special character from string in python using filter

The above Python code we can use to remove a special character from a string.

Python remove a character from a string in the list

  • In this section, we will learn how to remove a character from a string in the list.
  • String replace() method replaces a character from a string in the list.

Syntax:

Here is the Syntax of String replace

replace[
        
        old_Str1,
        new_Str2,
        
       ]
        

Let’s take an example to check how to remove a character from a String in the list

str2=['a','e','k','h']
str3="Micheal"
for i in str2:
    str3=str3.replace(i,"")
print(str3)

Here is the screenshot of the following given code

Remove a character from the string in the list
Remove a character from the string in the list

This is how to remove a character from a string in a Python list.

Python remove all instances of a character from a string

  • In this section, we will learn how to remove all instances of a character from a string.
  • String replace() method replaces a character from a string in the list.
  • For example, removing all instances of “a” in “California” we have to use the string replace() function.

Syntax:

Here is the Syntax of String replace.

replace[
        
        old_Str2,
        new_Str3,
        instance
       ]

Let’s take an example to check how to remove all instances of a character from a string.

str2="calfornia"
str3= str2.replace("a","")
print(str3)

Here is the screenshot of the following given code.

Remove all instances of a character from a string
Remove all instances of a character from a string

This is how to remove all instances of a character from a string in Python.

You may like the following Python tutorials:

In this tutorial, we learned how to remove characters from string Python.

  • How to remove a character from string in Python
  • Python remove a character from a string using replace() method
  • Python remove multiple characters from a string using replace() method
  • Python remove a character from a string using translate() method
  • Python remove multiple characters from a string using translate() method
  • Remove the first character from string python
  • Remove n character from string python
  • Remove newline from string python
  • Remove specified number of times in python
  • Python replace multiple characters in a string
  • Remove string from string python
  • How to remove punctuation from a string python
  • Remove the last character from string python
  • Remove last 4 characters from string python
  • Python remove all whitespace from a string
  • Python remove only leading and trailing spaces
  • Remove multiple characters from a string in python
  • How to remove spaces from string python
  • python strip substring from string
  • Remove a character from a string python by index
  • Remove a character from a string python pandas
  • Python remove a special character from the string
  • Python remove a character from a string in the list
  • Python remove all instances of a character from a string