Python Program to convert String to Uppercase under the Given Condition
Given a String list, the task is to write a Python program to convert uppercase strings if 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 task of uppercasing using upper(), and conditional statements for greater is checked using loop.
Python3
# Python3 code to demonstrate working of# Conditional Uppercase by size# Using upper() + loop # initializing listtest_list = ["Gfg", "is", "best", "for", "geeks"] # printing original listprint("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 resultprint("Modified Strings : " + str(res)) |
Output:
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks'] Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Method #2 : Using list comprehension
In this, task of iteration is performed inside list comprehension to act as shorthand to similar method as above.
Python3
# Python3 code to demonstrate working of# Conditional Uppercase by size# Using list comprehension # initializing listtest_list = ["Gfg", "is", "best", "for", "geeks"] # printing original listprint("The original list is : " + str(test_list)) # initializing K K = 3 # list comprehension for one liner solutionres = [ele.upper() if len(ele) > K else ele for ele in test_list] # printing resultprint("Modified Strings : " + str(res)) |
Output:
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks'] Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


