Python program: concatenate the Kth index words of strings

PHPz
Release: 2023-09-23 18:09:05
forward
991 people have browsed it

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)
Copy after login

Output

This
Copy after login

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)
Copy after login

Output

This a string test program
Copy after login
Copy after login

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)
Copy after login

Output

This a string test program
Copy after login
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!