Skip to content
Related Articles

Related Articles

Python program to print k characters then skip k characters in a string

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Aug, 2022
View Discussion
Improve Article
Save Article

Given a String, extract K characters alternatively.

Input : test_str = ‘geeksgeeksisbestforgeeks’, K = 4 
Output : geekksisforg 
Explanation : Every 4th alternate range is sliced.
Input : test_str = ‘geeksgeeksisbest’, K = 4 
Output : geekksis 
Explanation : Every 4th alternate range is sliced. 
 

Method #1 : Using loop + slicing 

In this, we perform task of getting K characters using slicing, and loop is used to perform task of concatenation.

Python3




# Python3 code to demonstrate working of
# Alternate K Length characters
# Using loop + slicing
 
# initializing string
test_str = 'geeksgeeksisbestforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 4
 
res = ''
 
# skipping k * 2 for altering effect
for idx in range(0, len(test_str), K * 2):
     
    # concatenating K chars
    res += test_str[idx : idx + K]
 
# printing result
print("Transformed String : " + str(res))

Output:

The original string is : geeksgeeksisbestforgeeks
Transformed String : geekksisforg

Method #2 : Using list comprehension + join()

This is similar to the above way, only difference being its one liner approach, and join() is used to perform task of convert back to string.

Python3




# Python3 code to demonstrate working of
# Alternate K Length characters
# Using list comprehension + join()
 
# initializing string
test_str = 'geeksgeeksisbestforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 4
 
# slicing K using slicing, join for converting back to string
res = ''.join([test_str[idx : idx + K] for idx in range(0, len(test_str), K * 2)])
 
# printing result
print("Transformed String : " + str(res))

Output:

The original string is : geeksgeeksisbestforgeeks
Transformed String : geekksisforg

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!