Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1.
Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul
We have existing solution for this problem in C++ please refer Replace a character c1 with c2 and c2 with c1 in a string S link. We will solve this problem quickly in Python using Lambda expression and map() function. We will create a lambda expression where character c1 in string will be replaced by c2 and c2 will be replaced by c1 and other will remain same, then we will map this expression on each character of string and will get updated string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
- Intersection of two arrays in Python ( Lambda expression and filter function )
- Lambda expression in Python to rearrange positive and negative numbers
- Python lambda (Anonymous Functions) | filter, map, reduce
- Check if a string contains only alphabets in Java using Lambda expression
- Replace minimal number of characters to make all characters pair wise distinct
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Nested Lambda Function in Python
- replace() in Python to replace a substring
- Python | Pandas Series.str.replace() to replace text in a series
- Lambda and filter in Python Examples
- Difference between List comprehension and Lambda in Python
- Overuse of lambda expressions in Python
- Python | Find fibonacci series upto n using lambda
- Python | sympy.Lambda() method
- Python lambda
- Python Program to Sort the list according to the column using lambda
- Python Lambda with underscore as an argument
- Applying Lambda functions to Pandas Dataframe
- Python | Replace multiple characters at once
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.