Python - Every Kth Strings Uppercase
Last Updated :
21 Apr, 2023
Given a String list, change every Kth string to uppercase.
Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 3
Output : ['GFG', 'is', 'best', 'FOR', 'geeks']
Explanation : All Kth strings are uppercased.
Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 4
Output : ['GFG', 'is', 'best', 'for', 'GEEKS']
Explanation : All Kth strings are uppercased.
Method #1 : Using loop + upper()
In this, we iterate for all strings using loop and upper is used to perform uppercase, Kth index is detected using modulo operator.
Python3
# Python3 code to demonstrate working of
# Every Kth Strings Uppercase
# Using loop + upper()
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks", "and", "CS"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
res = []
for idx in range(len(test_list)):
# checking for Kth index
if idx % K == 0:
res.append(test_list[idx].upper())
else :
res.append(test_list[idx])
# printing result
print("The resultant String list : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']
Time Complexity: O(n), where n is length of test_list.
Auxiliary Space: O(n), where n is length of res list to store the result.
Method #2: Using list comprehension
This is yet another way in which this task can be performed. In this we use list comprehension as shorthand, performs tasks similar to the above method.
Python3
# Python3 code to demonstrate working of
# Every Kth Strings Uppercase
# Using list comprehension
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks", "and", "CS"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
# shorthand to perform this task
res = [test_list[idx].upper() if idx % K == 0 else test_list[idx]
for idx in range(len(test_list))]
# printing result
print("The resultant String list : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using reduce():
Algorithm:
- Initialize the list test_list with the given strings.
- Print the original list test_list.
- Initialize the value of K to 3.
- Define a lambda function to_upper that takes an index and an element as input and returns the uppercase
- version of the element if the index is a multiple of K, otherwise returns the original element.
- Use the reduce method from the functools module to apply the lambda function to each element of the
- test_list and accumulate the results in a list called res.
- Print the resulting uppercase string list res.
Python3
# Python3 code to demonstrate working of
# Every Kth Strings Uppercase
# Using reduce method
# import reduce from functools module
from functools import reduce
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks", "and", "CS"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
# define lambda function to convert strings to uppercase
to_upper = lambda idx, elem: elem.upper() if idx % K == 0 else elem
# apply lambda function to each element of the list using reduce method
res = reduce(lambda acc, elem: acc + [to_upper(len(acc), elem)], test_list, [])
# printing result
print("The resultant String list : " + str(res))
#This code is contributed by Vinay pinjala.
OutputThe original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']
Time Complexity: O(n), where n is the length of the input list test_list. The reduce method applies the to_upper lambda function to each element of the test_list exactly once, so the time complexity of the reduce method is O(n).
Space Complexity: O(n). The test_list and res lists both have a size of n, where n is the length of the input list. The to_upper lambda function requires constant space, so its space complexity is O(1).
Similar Reads
Python - Uppercase Half String
The problem is to convert half of a string to uppercase, either the first half or the second half, depending on the requirement. For example, given the string "python", the output could be "PYThon" (uppercase first half) or "pytHON" (uppercase second half).If the string length is odd, handle the mid
2 min read
Python - Random uppercase in Strings
Given a String, the task is to write a Python program to convert its characters to uppercase randomly. Examples: Input : test_str = 'geeksforgeeks' Output : GeeksfORgeeks Explanation : Random elements are converted to Upper case characters. Input : test_str = 'gfg' Output : GFg Explanation : Random
4 min read
Python - Filter Supersequence Strings
Given a Strings list and substring, the task is to write a Python program to extract all the strings that has all the characters that can be used to make a substring. Examples: Input : test_list = ["gfg", "/", "geeksforgeeks", "best", "for", "geeks"], substr = "kgs" Output : ["geeksforgeeks", "geeks
4 min read
Python - Uppercase Nth character
The problem of capitalizing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the Nth character of the string to uppercase. Letâs discuss certain ways in which this can be performed. Method #1 : Using string slici
5 min read
Python - Sort by Uppercase Frequency
Given a list of strings, perform sorting by frequency of uppercase characters. Input : test_list = ["Gfg", "is", "FoR", "GEEKS"] Output : ['is', 'Gfg', 'FoR', 'GEEKS'] Explanation : 0, 1, 2, 5 uppercase letters in strings respectively. Input : test_list = ["is", "GEEKS"] Output : ['is', 'GEEKS'] Exp
5 min read
Python - Kth Valid String
Sometimes while dealing with data science, we need to handle a large amount of data and hence we may require shorthands to perform certain tasks. We handle the Null values at preprocessing stage and hence sometimes require to check for the Kth valid element. Letâs discuss certain ways in which we ca
3 min read
Python - Test if String contains any Uppercase character
The goal is to check if a given string contains at least one uppercase letter (A-Z). Using any() and isupper()any() function, combined with isdigit(), checks if any character in a string is a digit. It efficiently scans the string and returns True if at least one digit is found.Python# Define the in
3 min read
Ways to split strings on Uppercase characters - Python
Splitting strings on uppercase characters means dividing a string into parts whenever an uppercase letter is encountered.For example, given a string like "CamelCaseString", we may want to split it into ["Camel", "Case", "String"]. Let's discuss different ways to achieve this.Using Regular Expression
3 min read
Python - Convert Strings to Uppercase in Dictionary Value Lists
In Python, sometimes a dictionary contains lists as its values, and we want to convert all the string elements in these lists to uppercase. For example, consider the dictionary {'a': ['hello', 'world'], 'b': ['python', 'programming']}. We want to transform it into {'a': ['HELLO', 'WORLD'], 'b': ['PY
3 min read
Python - Uppercase Selective Substrings in String
Given a String, perform uppercase of particular Substrings from List. Input : test_str = 'geeksforgeeks is best for cs', sub_list = ["best", "geeksforgeeks"] Output : GEEKSFORGEEKS is BEST for cs Explanation : geeksforgeeks and best uppercased. Input : test_str = 'geeksforgeeks is best for best', su
7 min read