In this tutorial you will learn writing program to remove a given character from a string in python.
For Example:
Input String: Quescol
Input Character : e
Output: Quscol (After removing 'e')
How our program will behave?
You have to give a string to the program during the execution and also a character which you want to remove from the given string.
Remove character from string in Python
str = input("please enter a string : ")
ch = input("please enter a character : ")
print(str.replace(ch," "))
Output:
please enter a string : quescol
please enter a character : e
qu scol
There are Two approach to write a program to remove a given character from string in Python.
1). Using replace() method
2). Using translate() method
Using replace() method
Using replace() method of python we can easily remove a given character from string.
replace() function takes two parameter where first parameter is the character which we want to remove or replace and second parameter is new value which will replace the first parameter from string.
Here we want to remove a given character. So we will replace the character which we want to remove with white space.
Lets see Program below
Remove character from string using replace() method in python
def remove_char(s1,s2):
print(s1.replace(s2, ''))
s1 = input("please give a String : ")
s2 = input("please give a Character to remove : ")
remove_char(s1,s2)
Output:
please give a String : quescol
please give a Character to remove : s
quecol
Using translate() method
Using translate() method in python we can also remove all occurrence of a given character from string.
The translate() method in python returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table.
And to use dictionary we have to use ascii codes instead of characters.
For this purpose we will use ord() function which takes a character as an input and convert it to ascii.
Lets see program
Remove character from string using translate() method in python
def remove_char(s1,s2):
dict = {ord(s2) : None}
print(s1.translate(dict))
s1 = input("please give a String : ")
s2 = input("please give a Character to remove : ")
remove_char(s1,s2)
Output
please give a String : quescol
please give a Character to remove : e
quscol
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…