Python program to Uppercase selective indices
Given a String perform uppercase to particular indices.
Input : test_str = ‘geeksgeeksisbestforgeeks’, idx_list = [5, 7, 3, 2, 6, 9]
Output : geEKsGEEkSisbestforgeeks
Explanation : Particular indices are uppercased.
Input : test_str = ‘geeksgeeksisbestforgeeks’, idx_list = [5, 7, 3]
Output : geeKsGeEksisbestforgeeks
Explanation : Particular indices are uppercased.
Method #1 : Using loop + upper()
In this, we perform the task of converting to uppercase using upper(), and convert to uppercase by checking indices from list.
Python3
test_str = 'geeksgeeksisbestforgeeks'
print ( "The original string is : " + str (test_str))
idx_list = [ 5 , 7 , 3 , 2 , 6 , 9 ]
res = ''
for idx in range ( 0 , len (test_str)):
if idx in idx_list:
res + = test_str[idx].upper()
else :
res + = test_str[idx]
print ( "Transformed String : " + str (res))
|
Output
The original string is : geeksgeeksisbestforgeeks
Transformed String : geEKsGEEkSisbestforgeeks
Method #2 : Using list comprehension + upper() + join()
A similar method as above, the difference being list comprehension is used to offer one-liner, and join() is used to convert back to a string.
Python3
test_str = 'geeksgeeksisbestforgeeks'
print ( "The original string is : " + str (test_str))
idx_list = [ 5 , 7 , 3 , 2 , 6 , 9 ]
res = ''.join([test_str[idx].upper() if idx in idx_list else test_str[idx]
for idx in range ( 0 , len (test_str))])
print ( "Transformed String : " + str (res))
|
Output
The original string is : geeksgeeksisbestforgeeks
Transformed String : geEKsGEEkSisbestforgeeks
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using index() and join() methods
Python3
test_str = 'geeksgeeksisbestforgeeks'
print ( "The original string is : " + str (test_str))
idx_list = [ 5 , 7 , 3 , 2 , 6 , 9 ]
loweralphabets = "abcdefghijklmnopqrstuvwxyz"
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res = ''
test_str = list (test_str)
for i in idx_list:
test_str[i] = upperalphabets[loweralphabets.index(test_str[i])]
print ( "Transformed String : " + "".join(test_str))
|
Output
The original string is : geeksgeeksisbestforgeeks
Transformed String : geEKsGEEkSisbestforgeeks
Method #4 : Using Map and Enumerate, Lambda Function
This method uses the built-in enumerate function to keep track of the indices while looping through the string. It then uses a lambda function to check if the current index is present in the idx_list, and if so, converts the corresponding character to uppercase. Finally, the result is obtained by converting the list of characters back to a string using join.
Python3
test_str = 'geeksgeeksisbestforgeeks'
print ( "The original string is : " + str (test_str))
idx_list = [ 5 , 7 , 3 , 2 , 6 , 9 ]
res = ''.join( list ( map ( lambda x: x[ 1 ].upper() if x[ 0 ] in idx_list else x[ 1 ], enumerate (test_str))))
print ( "Transformed String : " + str (res))
|
Output
The original string is : geeksgeeksisbestforgeeks
Transformed String : geEKsGEEkSisbestforgeeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5 : utilizing the string slicing method
step-by-step approach
- Initialize a string variable test_str with the value “geeksgeeksisbestforgeeks”.
- Initialize a list variable idx_list with the values [5, 7, 3, 2, 6, 9]. These values represent the indices of the characters in test_str that should be converted to uppercase.
- Create a new string variable new_str and set its value to an empty string. This variable will be used to store the transformed string.
- Loop through each index in the range from 0 to the length of the test_str minus 1. The range() function is used to generate a sequence of integers from 0 up to, but not including, the length of test_str.
- Inside the loop, check if the current index is in the list of indices to convert (idx_list). If it is, append the uppercase version of the character at that index to new_str using the upper() method. If it’s not, append the original character at that index to new_str.
- After the loop is finished, the new_str variable will contain the transformed string. Print this string with a message to indicate that it is the transformed string.
Python3
test_str = 'geeksgeeksisbestforgeeks'
idx_list = [ 5 , 7 , 3 , 2 , 6 , 9 ]
new_str = ''
for idx in range ( len (test_str)):
if idx in idx_list:
new_str + = test_str[idx].upper()
else :
new_str + = test_str[idx]
print ( "Transformed String: " + new_str)
|
Output
Transformed String: geEKsGEEkSisbestforgeeks
Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(n), as the new string variable requires additional space to store the transformed string.
Method 6: Using numpy:
Algorithm:
- Initialize a string (test_str) and a list of indices (idx_list).
- Create a lambda function that takes in an enumerated tuple (i.e., a tuple with the index and the corresponding
- character of the string) and returns the uppercase character if the index is in idx_list, or the original character otherwise.
- Use the map() function with the lambda function and the enumerated string (using the enumerate() function)
- to apply the transformation to each character of the string.
- Join the resulting list of characters into a string.
- Print the transformed string.
Python3
import numpy as np
test_str = 'geeksgeeksisbestforgeeks'
print ( "The original string is : " + str (test_str))
idx_list = [ 5 , 7 , 3 , 2 , 6 , 9 ]
arr = np.array( list (test_str))
mask = np.zeros_like(arr, dtype = bool )
mask[idx_list] = True
arr[mask] = np.char.upper(arr[mask])
res = ''.join(arr)
print ( "Transformed String : " + str (res))
|
Output:
The original string is : geeksgeeksisbestforgeeks
Transformed String : geEKsGEEkSisbestforgeeks
Time complexity:
The enumerate() function has a time complexity of O(n), where n is the length of the string.
The lambda function executes in constant time O(1) for each tuple in the enumerated string.
The map() function applies the lambda function to each tuple, resulting in a total time complexity of O(n).
The join() function has a time complexity of O(n), where n is the length of the string.
Therefore, the overall time complexity of the algorithm is O(n).
Space complexity:
The space complexity of the algorithm is O(n), where n is the length of the string. This is because we need to store the original string, the transformed string, the list of indices, and the temporary tuples created by enumerate(). The size of these data structures scales linearly with the size of the input string.
Last Updated :
11 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...