Home  >  Article  >  Backend Development  >  Python program: concatenate the Kth index words of strings

Python program: concatenate the Kth index words of strings

PHPz
PHPzforward
2023-09-23 18:09:05967browse

Python program: concatenate the Kth index words of strings

String is an immutable data structure that stores data in string format. It can be created by using the str() method or by giving the data in single quotes or double quotes . It accesses the elements of the string we use the index. In indexing we have negative index and positive index, like with negative index we will access the last element to the first element using -1 and (-length of string ). In positive indexing, we will assign 0 to the first element and (string length - 1) to the last element.

Now, in this article, we will concatenate the Kth index term of a string using different methods available in Python. Let’s learn about each method in detail.

Use loop

In this method, we split the input string into a list of words using the split() method. We then iterate over the words and check if the index is a multiple of k. If so, we concatenate the words with spaces into the resulting string. Finally, we use the strip() method to remove any leading or trailing spaces from the resulting string.

Example

def concatenate_kth_words(string, k):
   words = string.split()  
   result = ""
   for i in range(len(words)):
      if i % k == 0: 
         result += words[i] + " "
      return result.strip()  
my_string = "This is a sample string to test the program"
k = 2
concatenated_words = concatenate_kth_words(my_string, k)
print(concatenated_words)

Output

This

Use list comprehension and join() function

In this approach, we use a list comprehension to create a new list containing only words with indexes that are multiples of k. We then use the join() method to concatenate the elements of the new list into a single string, separating them with spaces.

Example

def concatenate_kth_words(string, k):
   words = string.split()  
   result = " ".join([words[i] for i in range(len(words)) if i % k == 0])
   return result
my_string = "This is a sample string to test the program"
k = 2
concatenated_words = concatenate_kth_words(my_string, k)
print(concatenated_words)

Output

This a string test program

Using slice and join() functions

In this method, we use list slicing to extract words whose index is a multiple of k. Slice words[::k]Starting from the first element, select every kth element. We then use the join() method to concatenate the selected words into a string, separated by spaces.

Example

def concatenate_kth_words(string, k):
   words = string.split()  # Split the string into a list of words
   result = " ".join(words[::k])
   return result
my_string = "This is a sample string to test the program"
k = 2
concatenated_words = concatenate_kth_words(my_string, k)
print(concatenated_words)

Output

This a string test program

The above is the detailed content of Python program: concatenate the Kth index words of strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete