Python | Alternate character addition

Sometimes, while working with Python, we can have a problem in which we need to add a character after every character in String. This kind of problem can have its application in many day-day programming domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop
This is brute force way in which this task can be performed. In this, we iterate for each element and insert the character required. We erase tha last stray character.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Alternate character addition
# Using loop
  
# initializing string
test_str = "geeksforgeeks"
  
# printing original string
print("The original string is : " + test_str)
  
# initializing K 
K = '*'
  
# Alternate character addition
# Using loop
res = ''
for ele in test_str:
    res += ele + K
res = res[:-1]
  
# printing result 
print("String after character addition : " + str(res)) 

chevron_right


Output :

The original string is : geeksforgeeks
String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s

 

Method #2 : Using join()
This is easiest, elegant and recommended way to perform this task. In this, we just use one line to perform this task. The join() is used to perform it.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Alternate character addition
# Using join()
  
# initializing string
test_str = "geeksforgeeks"
  
# printing original string
print("The original string is : " + test_str)
  
# initializing K 
K = '*'
  
# Alternate character addition
# Using join()
res = K.join(test_str)
  
# printing result 
print("String after character addition : " + str(res)) 

chevron_right


Output :

The original string is : geeksforgeeks
String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.