Sometimes, we can have a problem in which we need to insert a specific character after pair(second) character. This kind of problem can occur while working with data, that require insertion of commas or any other special character mainly in Machine Learning domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using
join() + list comprehension
The combination of above method can be used to perform this particular task. List comprehension along with slicing can be used to convert string to list and join function can be used to rejoin them inserting required character between them.
The original string is : GeeksforGeeks The string after inserting comma after every character pair : Ge, ek, sf, or, Ge, ek, s
Method #2 : Using
zip() + join()
The combination of above functions can be used to perform this particular task. In this, zip function converts the characters to iterable tuples, split function is used to separate odd and even characters. Then list comprehension is responsible to convert the tuples to list of strings and at last result is joined using the join function.
The original string is : GeeksforGeeks The string after inserting comma after every character pair : Ge, ek, sf, or, Ge, ek
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Insert character in each duplicate string after every K elements
- Python | Insert after every Nth element in a list
- Python | Insert value after each k letters in given list of string
- Python | Split K elements after every N values
- Python program to covert tuple into list by adding the given string after every element
- Python | Pair the consecutive character strings in a list
- Python | Pair Kth character with each element
- Python | Find frequency of given character at every position in list of lists
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
- Python | Pandas Index.insert()
- MongoDB Python | Insert and Update Data
- Python list | insert()
- Python | Pandas dataframe.insert()
- Python | Insert the string at the beginning of all items in a list
- Python | Pandas TimedeltaIndex.insert
- Python program to insert an element into sorted list
- Python | Insert list in another list
- Python | Split given list and insert in excel file
- Python | Insert a number in string
- Python | Insert Nth element to Kth element in other list
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.