Open In App
Related Articles

Python program to Concatenate Kth index words of String

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a string with words, concatenate the Kth index of each word.

Input : test_str = ‘geeksforgeeks best geeks’, K = 3 
Output : ktk 
Explanation : 3rd index of “geeksforgeeks” is k, “best” has ‘t’ as 3rd element.

Input : test_str = ‘geeksforgeeks best geeks’, K = 0 
Output : gbg 

Method #1 : Using join() + list comprehension + split()

In this, we perform the task of splitting to get all the words and then use list comprehension to get all Kth index of words, join() is used to perform concatenation.

Step-by-step approach:

  1. Use the split() method to split the input string into a list of words.
  2. Use a list comprehension to iterate over each word in the list obtained in step 4 and select the Kth character of each word.
  3. Use the join() method to concatenate the Kth characters of all the words in the list obtained in step 5.
  4. Store the concatenated string in a variable called res.
  5. Print the concatenated string using the print() function and string concatenation.

Below is the implementation of the above approach:

Python3

# initializing string
test_str = 'geeksforgeeks best for geeks'
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 2
 
# joining Kth index of each word
res = ''.join([sub[K] for sub in test_str.split()])
     
# printing result
print("The K joined String is : " + str(res))

                    

Output
The original string is : geeksforgeeks best for geeks
The K joined String is : esre

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

Method #2 : Using loop + join()

Step-by-step approach:

  1. Initializes an integer variable K.
  2. Creates an empty list temp.
  3. Uses the split() method to split the test_str variable into a list of individual words. It then iterates over each word in the list using a for loop.
  4. For each word, the program appends the Kth character of the word to the temp list using the append() method.
  5. Once all words have been processed, the program joins the elements of temp together into a single string using the join() method and assigns the result to a variable named res.
  6. Finally, the program prints the resulting string using the print() function with the message “The K joined String is : ” concatenated with the string representation of the res variable.

Below is the implementation of the above approach:

Python3

# initializing string
test_str = 'geeksforgeeks best for geeks'
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 2
 
# getting Kth element of each word
temp = []
for sub in test_str.split():
  temp.append(sub[K])
 
# joining together 
res = ''.join(temp)
     
# printing result
print("The K joined String is : " + str(res))

                    

Output
The original string is : geeksforgeeks best for geeks
The K joined String is : esre

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

Method 3: Using map() + lambda function + str.join() + str.split()

Step-by-step approach:

  • Initialize the variable “K” with the index of the character to be extracted from each word.
  • Split the input string into a list of words using the “split()” method and store it in the variable “words”.
  • Use the “map()” function to apply a lambda function to each word in the list “words”.
  • The lambda function takes a word as input and returns the Kth character of that word.
  • Use the “join()” method to join the resulting list of characters into a single string.
  • Print the resulting string.

Below is the implementation of the above approach:

Python3

# initializing string
test_str = 'geeksforgeeks best for geeks'
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 2
 
# getting Kth element of each word
res = ''.join(map(lambda x: x[K], test_str.split()))
 
# printing result
print("The K joined String is : " + str(res))

                    

Output
The original string is : geeksforgeeks best for geeks
The K joined String is : esre

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.


 



Last Updated : 10 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads