How to delete a character from a string using python?


Here are some examples of how to delete a character from a string using Python.

Using String Slicing

One of the easiest ways to delete a character from a string in Python is to use string slicing. Here's how you can do it:

Example

In the first line of code, we define a string called "my_string" that contains the text "Hello World".

In the second line of code, we remove the character "o" by creating a new string called "new_string".

We do this by slicing the original string into two parts - the characters before the "o" and the characters after the "o".

We then concatenate these two parts together to form the new string.

Finally, the new string is printed to the console.

# Define a string
my_string = "Lorem Ipsum"

# Remove the character "m"
new_string = my_string[:4] + my_string[5:]

# Print the new string
print(new_string)

Output

Lore Ipsum

Using replace() Method

Another way to remove a character from a string in Python is to use the replace() method. Here's how you can do it:

Example

In the first line of code, we define a string called "my_string" that contains the text "Hello World".

In the second line of code, we remove the character "o" by using the replace() method.

The replace() method takes two arguments - the character to be replaced and the replacement string.

In our case, we want to replace the character "o" with an empty string, so we pass "" as the second argument.

The replace() method returns a new string with the specified character(s) replaced.

Finally, we print the new string to the console.

# Define a string
my_string = "Lorem Ipsum"

# Remove the character "m"
new_string = my_string.replace("m", "")

# Print the new string
print(new_string)

Output

Lore Ipsu

Using join() and list comprehension

Another way to remove a character from a string in Python is to convert the string into a list of characters, remove the desired character using list comprehension, and then convert the list back to a string using the join() method. Here's how you can do it:

Example

In the first line of code, we define a string called "my_string" that contains the text "Hello World".

In the second line of code, we convert the string into a list of characters using a list comprehension.

The list comprehension loops through each character in the string and appends it to the char_list variable if it is not equal to "o".

In the third line of code, we convert the list back to a string using the join() method.

The join() method takes a sequence of strings as its argument and concatenates them into a single string, separated by the string on which it is called (in our case, an empty string).

Finally, the new string is printed to the console.

# Define a string
my_string = "Lorem Ipsum"
# Remove the character "m"
char_list = [char for char in my_string if char != "m"]
new_string = "".join(char_list)

# Print the new string
print(new_string)

Output

Lore Ipsu

Using translate() method

Another way to remove a character from a string in Python is to use the translate() method. Here's how you can do it:

Example

In the first line of code, we define a string called "my_string" that contains the text "Hello World".

In the second line of code, we remove the character "o" using the translate() method.

The translate() method takes a dictionary that maps each character to its replacement character, and replaces all occurrences of each character in the string with its replacement character.

In our case, we want to replace the character "o" with nothing (i.e., remove it), so we pass ord("o"): None as the dictionary.

The ord() function is used to convert a character to its corresponding Unicode code point, which is the key in the dictionary.

Finally, we print the new string to the console.

# Define a string
my_string = "Lorem Ipsum"

# Remove the character "m"
new_string = my_string.translate({ord("m"): None})

# Print the new string
print(new_string)

Output

Lore Ipsu

Using re.sub() function

Another way to remove a character from a string in Python is to use the re.sub() function from the re module. Here's how you can do it:

Example

In the first line of code, we import the re module, which provides support for regular expressions in Python.

In the second line of code, we define a string called "my_string" that contains the text "Hello World".

In the third line of code, we remove the character "o" using the re.sub() function.

The re.sub() function takes three arguments - the pattern to search for, the replacement string, and the input string.

In our case, we want to replace the character "o" with an empty string, so we pass "" as the second argument.

The re.sub() function returns a new string with all occurrences of the pattern replaced with the replacement string.

Finally, the new string is printed to the console.

import re

# Define a string
my_string = "Lorem Ipsum"
# Remove the character "m"
new_string = re.sub("m", "", my_string)

# Print the new string
print(new_string)

Output

Lore Ipsu

Using list comprehension and join() method

Another way to remove a character from a string in Python is to convert the string into a list of characters, remove the desired character using list comprehension, and then convert the list back to a string using the join() method. Here's how you can do it:

Example

In the first line of code, we define a string called "my_string" that contains the text "Hello World".

In the second line of code, we convert the string into a list of characters using a list comprehension.

The list comprehension loops through each character in the string and appends it to the char_list variable if it is not equal to "o".

In the third line of code, we convert the list back to a string using the join() method, which concatenates all the characters in the list and returns a new string.

Finally, we print the new string to the console.

# Define a string
my_string = "Lorem Ipsum"

# Remove the character "o"
char_list = [char for char in my_string if char != "m"]
new_string = "".join(char_list)

# Print the new string
print(new_string)

Output

Lore Ipsu

Updated on: 10-Aug-2023

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements