Python | Get positional characters from String

Sometimes, while working with Python strings, we can have a problem in which we need to create a substring by joining the particular index elements from a string. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop This is a brute method in which this task can be performed. In this, we run a loop over the indices list and join the corresponding index characters from string.
Python3
# Python3 code to demonstrate working of# Get positional characters from String# using loop# initializing stringtest_str = "gfgisbest"# printing original stringprint("The original string is : " + test_str)# initializing index listindx_list = [1, 3, 4, 5, 7]# Get positional characters from String# using loopres = ''for ele in indx_list: res = res + test_str[ele]# printing resultprint("Substring of selective characters : " + res) |
The original string is : gfgisbest Substring of selective characters : fisbs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using generator expression + enumerate() The combination of above functionalities can be used to perform this task. In this, we run a loop using generator expression and indices extraction is done with help of enumerate().
Python3
# Python3 code to demonstrate working of# Get positional characters from String# using generator expression + enumerate()# initializing stringtest_str = "gfgisbest"# printing original stringprint("The original string is : " + test_str)# initializing index listindx_list = [1, 3, 4, 5, 7]# Get positional characters from String# using generator expression + enumerate()res = ''.join((char for idx, char in enumerate(test_str) if idx in indx_list))# printing resultprint("Substring of selective characters : " + res) |
The original string is : gfgisbest Substring of selective characters : fisbs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using for loop
Python3
# Python3 code to demonstrate working of# Get positional characters from String# using loop# initializing stringtest_str = "gfgisbest"# printing original stringprint("The original string is : " + test_str)# initializing index listindx_list = [1, 3, 4, 5, 7]# Get positional characters from String# using loopres = ''for i in range(0, len(test_str)): if i in indx_list: res += test_str[i]# printing resultprint("Substring of selective characters : " + res) |
The original string is : gfgisbest Substring of selective characters : fisbs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using for list comprehension
Python3
# Python3 code to demonstrate working of# Get positional characters from String# initializing stringtest_str = "gfgisbest"# printing original stringprint("The original string is : " + test_str)# initializing index listindx_list = [1, 3, 4, 5, 7]# printing resultprint("Substring of selective characters : ", end=' ')resultstring = ""reslist = [resultstring+test_str[i] for i in indx_list]print(''.join(reslist)) |
The original string is : gfgisbest Substring of selective characters : fisbs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using map() and lambda function
This method uses the map() function and a lambda function to extract the characters at the specified indices and join them to create the substring.
Python3
# Python3 code to demonstrate working of# Get positional characters from String# using map() and lambda function # initializing stringtest_str = "gfgisbest" # printing original stringprint("The original string is : " + test_str) # initializing index listindx_list = [1, 3, 4, 5, 7] # Get positional characters from String# using map() and lambda functionres = ''.join(list(map(lambda x: test_str[x], indx_list)))# printing resultprint("Substring of selective characters : " + res)#This code is contributed by Edula Vinay Kumar Reddy |
The original string is : gfgisbest Substring of selective characters : fisbs
Time Complexity: O(n) where n is the number of elements in the index list
Auxiliary Space: O(n) as it uses a list to store the characters at the specified indices.
Method 6 : using the reduce() function from the functools module.
step by step:
- Import the reduce() function from the functools module.
- Initialize the test_str and indx_list variables as before.
- Define a lambda function that takes two arguments: a string and an index. The lambda function returns
- the concatenation of the string with the character at the given index.
- Use the reduce() function to apply the lambda function to each element of the indx_list, starting with an empty string.
- Print the result:
Python3
from functools import reduce# initializing stringtest_str = "gfgisbest"# initializing index listindx_list = [1, 3, 4, 5, 7]# define lambda functionf = lambda x, i: x + test_str[i]# apply lambda function using reduceres = reduce(f, indx_list, "")# printing resultprint("Substring of selective characters : " + res) |
Substring of selective characters : fisbs
The time complexity of this approach is O(n), where n is the length of the indx_list.
The auxiliary space complexity is O(1), as we are not using any extra space other than the input and output variables.



