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’, sub_list = [“best”, “geeksforgeeks”]
Output : GEEKSFORGEEKS is BEST for BEST
Explanation : geeksforgeeks and best both occurrences uppercased.

Method #1 : Using split() + join() + loop

In this, we repeatedly split the string by substring and then perform join operation after joining the String with uppercased version of Substring. This is success only in cases of 1 occurrence of substring in String.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Uppercase Selective Substrings in String
# Using split() + join() + loop
  
# initializing strings
test_str = 'geeksforgeeks is best for cs'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing substrings
sub_list = ["best", "cs", "geeksforgeeks"]
  
  
for sub in sub_list:
      
    # splitting string
    temp = test_str.split(sub, -1)
      
    # joining after uppercase
    test_str = sub.upper().join(temp)
  
  
# printing result 
print("The String after uppercasing : " + str(test_str)) 

chevron_right


Output

The original string is : geeksforgeeks is best for cs
The String after uppercasing : GEEKSFORGEEKS is BEST for CS

Method #2 : Using re.sub() + upper()

This uses regex to solve this problem. In this, we use appropriate regex, and perform uppercase of found Strings.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Uppercase Selective Substrings in String
# Using re.sub() + upper()
import re
  
# initializing strings
test_str = 'geeksforgeeks is best for cs'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing substrings
sub_list = ["best", "cs", "geeksforgeeks"]
  
# constructing regex
reg = '|'.join(sub_list)
res = re.sub(reg, lambda ele: ele.group(0).upper(), test_str)
  
# printing result 
print("The String after uppercasing : " + str(res)) 

chevron_right


Output

The original string is : geeksforgeeks is best for cs
The String after uppercasing : GEEKSFORGEEKS is BEST for CS



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.