Python Program to convert String to Uppercase under the Given Condition
Last Updated :
18 May, 2023
Given a String list, the task is to write a Python program to convert uppercase strings if the length is greater than K.
Examples:
Input : test_list = ["Gfg", "is", "best", "for", "geeks"], K = 3
Output : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Explanation : Best has 4 chars, hence BEST is uppercased.
Input : test_list = ["Gfg", "is", "best", "for", "geeks"], K = 4
Output : ['Gfg', 'is', 'best', 'for', 'GEEKS']
Explanation : geeks has 5 chars [greater than 4], hence GEEKS is uppercased.
Method #1 : Using upper() + loop
In this, we perform the task of uppercasing using upper(), and conditional statements for greater are checked using a loop.
Python3
# Python3 code to demonstrate working of
# Conditional Uppercase by size
# Using upper() + loop
# initializing list
test_list = ["Gfg", "is", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
res = []
for ele in test_list:
# check for size
if len(ele) > K:
res.append(ele.upper())
else:
res.append(ele)
# printing result
print("Modified Strings : " + str(res))
OutputThe original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
In this, the task of iteration is performed inside list comprehension to act as shorthand to the similar method as above.
Python3
# Python3 code to demonstrate working of
# Conditional Uppercase by size
# Using list comprehension
# initializing list
test_list = ["Gfg", "is", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
# list comprehension for one liner solution
res = [ele.upper() if len(ele) > K else ele for ele in test_list]
# printing result
print("Modified Strings : " + str(res))
OutputThe original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using map function + lambda function.
Python3
# Python3 code to demonstrate working of
# Conditional Uppercase by size
# using map function and lambda function
# Initializing list
test_list = ["Gfg", "is", "best", "for", "geeks"]
# Printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Using map and lambda function
res = list(map(lambda ele: ele.upper() if len(ele) > K else ele, test_list))
# Printing result
print("Modified Strings : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using map() and a named helper function
- Define a named function 'conditional_uppercase' that takes a string as an input parameter
- Inside the function, check if the length of the string is greater than 'K'
- If yes, return the uppercase version of the string
- If no, return the original string.
- Initialize an empty list called 'res'
- Use map() function to apply the 'conditional_uppercase' function to each element of the 'test_list'
- Convert the map object to a list using the list() function and store it in 'res'
- Print the modified strings stored in 'res'
Python3
# Python3 code to demonstrate working of
# Conditional Uppercase by size
# Using map() and a named function
# initializing list
test_list = ["Gfg", "is", "best", "for", "geeks"]
# Printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
# named function for conditional uppercase
def conditional_uppercase(ele):
return ele.upper() if len(ele) > K else ele
# Applying conditional_uppercase function to each element
# map function
res = list(map(conditional_uppercase, test_list))
# Printing the result
print("Modified Strings : " + str(res))
OutputThe original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time complexity: O(n), where n is the number of elements in the 'test_list'
Auxiliary space: O(n), as we are storing the modified strings in a new list 'res'
Method #6: Using generator function
Python3
# Python3 code to demonstrate working of
# Conditional Uppercase by size
# Using a generator function
# Initializing list
test_list = ["Gfg", "is", "best", "for", "geeks"]
# Printing original list
print("The original list is : " + str(test_list))
# Initializing K
K = 3
# Generator function for conditional uppercase
def conditional_uppercase_generator(lst, k):
for ele in lst:
yield ele.upper() if len(ele) > k else ele
# Creating a generator object
gen = conditional_uppercase_generator(test_list, K)
# Converting generator object to list
res = list(gen)
# Printing the result
print("Modified Strings : " + str(res))
OutputThe original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time complexity: O(n), since we need to iterate over all the elements in the list.
Auxiliary space: O(1) because we are not storing all the modified strings in a separate list. Instead, we generate them on-the-fly using the generator function.
Similar Reads
Python program to uppercase the given characters Given a string and set of characters, convert all the characters that occurred from character set in string to uppercase() Input : test_str = 'gfg is best for geeks', upper_list = ['g', 'e', 'b'] Output : GfG is BEst for GEEks Explanation : Only selective characters uppercased.Input : test_str = 'gf
7 min read
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 : geeKsGe
7 min read
Python Program to Converts Characters To Uppercase Around Numbers Given a String, the following program converts the alphabetic character around any digit to its uppercase. Input : test_str = 'geeks4geeks is best1 f6or ge8eks' Output : geekS4Geeks is besT1 F6Or gE8Ek Explanation : S and G are uppercased as surrounded by 4.Input : test_str = 'geeks4geeks best1 f6or
8 min read
Python - Get the indices of Uppercase characters in given string Given a String extract indices of uppercase characters. Input : test_str = 'GeeKsFoRGeeks' Output : [0, 3, 5, 7, 8] Explanation : Returns indices of uppercase characters. Input : test_str = 'GFG' Output : [0, 1, 2] Explanation : All are uppercase. Method #1 : Using list comprehension + range() + isu
5 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