Open In App

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

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Time Complexity: O(n)
Auxiliary Space: O(n) 

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)
Auxiliary Space: O(n)

Method #3: Using map and lambda function.

Python3




# Python3 code to demonstrate working of
# Alternate K Length characters
# Using map and lambda function:
 
# initializing string
test_str = 'geeksgeeksisbestforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 4
 
result = ''.join(map(lambda x: test_str[x:x+K], range(0, len(test_str), 2 * K)))
 
 
# printing result
print("Transformed String : " + str(result))
 
#this code contributed by tvsk.


Output

The original string is : geeksgeeksisbestforgeeks
Transformed String : geekksisforg

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Here’s an implementation using the reduce function from the functools module:

The reduce function applies the lambda function to the elements of the list and accumulates the results. In this case, the lambda function takes two arguments x and y, which are the previous and current elements in the list respectively, and concatenates them. The reduce function starts with the first two elements of the list and the result is the final concatenated string.

Python3




from functools import reduce
 
# initializing string
test_str = 'geeksgeeksisbestforgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 4
 
# using reduce to concatenate the K characters
result = reduce(lambda x, y: x + y, [test_str[i:i+K] for i in range(0, len(test_str), 2 * K)])
 
# printing result
print("Transformed String : " + str(result))


Output

The original string is : geeksgeeksisbestforgeeks
Transformed String : geekksisforg

The time and auxiliary space for this implementation will also be O(n).

Method 5 :  using a generator function

step-by-step approach

  1. Define a function named chunk_generator that takes two arguments: a string s and an integer k.
  2. The function uses a for loop with a range of 0 to the length of the input string, with a step of k*2. This skips 2*k characters each time to alternate between the chunks.
  3. The loop yields a slice of the string, starting from index i and going up to index i+k. This slice contains a chunk of k characters from the input string.
  4. In the main program, initialize a string test_str and an integer K.
  5. Call the chunk_generator function with the test_str and K arguments. This generates a generator object that yields chunks of K characters.
  6. Use the join method to concatenate the chunks into a single string, and assign the result to a variable named res.
  7. Print the resulting string, with the message “Transformed String : ” concatenated to the beginning of the string.

Python3




# defining chunk generator function
def chunk_generator(s, k):
    for i in range(0, len(s), k*2):
        yield s[i:i+k]
 
# initializing string and K
test_str = 'geeksgeeksisbestforgeeks'
K = 4
 
# generating chunks and joining them together
res = ''.join(chunk_generator(test_str, K))
 
# printing result
print("Transformed String : " + str(res))


Output

Transformed String : geekksisforg

Time complexity: The program has a time complexity of O(n/k), where n is the length of the input string and k is the length of each chunk. 

Auxiliary space complexity: The program has an auxiliary space complexity of O(k), which is the size of each chunk.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads