Open In App

Python program to uppercase the given characters

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 = 'gfg is best', upper_list = ['g', 'e', 'b'] 
Output : GfG is BEst 
Explanation : Only selective characters uppercased. 

Method #1 :  Using loop + upper()

The combination of the above functions can be used to solve this problem. In this, we check for each character using a loop, if it is in the characters, it is converted to uppercase using upper().

Python3




# Python3 code to demonstrate working of
# Uppercase custom characters
# Using upper() + loop
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing upperlist
upper_list = ['g', 'e', 'b', 'k']
 
res = ''
for ele in test_str:
 
    # checking for presence in upper list
    if ele in upper_list:
        res += ele.upper()
    else:
        res += ele
 
# printing result
print("String after reconstruction : " + str(res))


Output

The original string is : gfg is best for geeks
String after reconstruction : GfG is BEst for GEEKs

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using list comprehension

This is yet another way in which this task can be performed. In this, we perform the task in a similar way as the above method, but as a shorthand using one-liner list comprehension.

Python3




# Python3 code to demonstrate working of
# Uppercase custom characters
# Using list comprehension
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing upperlist
upper_list = ['g', 'e', 'b', 'k']
 
# one-liner used to solve problem
res = "".join([ele.upper() if ele in upper_list else ele for ele in test_str])
 
# printing result
print("String after reconstruction : " + str(res))


Output

The original string is : gfg is best for geeks
String after reconstruction : GfG is BEst for GEEKs

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using replace() and upper() methods

Step-by-step approach:

  • Initialize a string ‘test_str‘ with some value.
  • Print the original value of ‘test_str‘ using the print statement.
  • Initialize a list ‘upper_list‘ with some custom characters in lowercase.
  • Loop over the elements in ‘upper_list‘ using the for loop.
    • In each iteration of the for loop, replace every occurrence of the current element with its uppercase version in the ‘test_str‘ string using the string method replace().
  • After the loop is complete, print the modified value of ‘test_str‘ using the print statement.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Uppercase custom characters
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing upperlist
upper_list = ['g', 'e', 'b', 'k']
 
# one-liner used to solve problem
 
for i in upper_list:
    test_str = test_str.replace(i, i.upper())
# printing result
print("String after reconstruction : " + str(test_str))


Output

The original string is : gfg is best for geeks
String after reconstruction : GfG is BEst for GEEKs

Time complexity: O(n*m), where n is the length of the string and m is the length of the upper_list.
Auxiliary Space: O(1), as the amount of extra space used does not depend on the input size.

Method #4 : Using replace() and index() methods

Python3




# Python3 code to demonstrate working of
# Uppercase custom characters
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing upperlist
upper_list = ['g', 'e', 'b', 'k']
loweralphabets="abcdefghijklmnopqrstuvwxyz"
upperalphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# one-liner used to solve problem
 
for i in upper_list:
    test_str=test_str.replace(i,upperalphabets[loweralphabets.index(i)])
# printing result
print("String after reconstruction : " + str(test_str))


Output

The original string is : gfg is best for geeks
String after reconstruction : GfG is BEst for GEEKs

Time complexity: O(n * m), where n is the length of the input string and m is the length of the upper_list.
Auxiliary space: O(1), as the replacements are done in-place without creating any new data structures.

Method #5: Using a dictionary to map custom characters to uppercase letters

Use a dictionary to map each custom character to its corresponding uppercase letter, and then use the str.translate() method to replace all occurrences of the custom characters with their uppercase equivalents. Here’s an example:

STEPS:

  1. Initialize a string.
  2. Print the original string.
  3. Initialize a list of custom characters to be converted to uppercase.
  4. Create a dictionary called “mapping” to map each custom character to its uppercase equivalent. This is done using dictionary comprehension, where the keys are the custom characters and the values are their uppercase equivalents.
  5. Use the translate() method to replace all occurrences of the custom characters in the string with their uppercase equivalents. The translate() method takes a dictionary as an argument, where the keys are the characters to be replaced and the values are the replacement characters. In this case, we pass the “mapping” dictionary as an argument to the translate() method.
  6. Print the result.
     

Python3




# Python3 code to demonstrate working of
# Uppercase custom characters
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing upperlist
upper_list = ['g', 'e', 'b', 'k']
 
# create a dictionary to map custom characters to uppercase letters
mapping = {c: c.upper() for c in upper_list}
 
# use str.translate() to replace all occurrences of custom characters with their uppercase equivalents
test_str = test_str.translate(str.maketrans(mapping))
 
# printing result
print("String after reconstruction : " + str(test_str))


Output

The original string is : gfg is best for geeks
String after reconstruction : GfG is BEst for GEEKs

Time complexity: O(n), where n is the length of the input string test_str.
Auxiliary space: O(k), where k is the number of custom characters that need to be converted to uppercase.

Method 6: Using the re.sub() function from the built-in Python module ‘re’, which allows replacing a specific pattern in a string with a new value. 

Step-by-step approach:

  • Import the ‘re’ module.
  • Define a function that takes a string and a list of custom characters, and returns a modified string where the custom characters are in uppercase.
  • Use the re.sub() function inside the function to replace each custom character with its uppercase equivalent.
  • Call the function with the original string and the list of custom characters as arguments.
  • Print the modified string.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Uppercase custom characters
 
import re
 
# initializing string
test_str = 'gfg is best for geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing upperlist
upper_list = ['g', 'e', 'b', 'k']
 
# define function to uppercase custom characters
def uppercase_custom_chars(s, custom_chars):
    for c in custom_chars:
        s = re.sub(c, c.upper(), s)
    return s
 
# call function with original string and custom characters
test_str = uppercase_custom_chars(test_str, upper_list)
 
# printing result
print("String after reconstruction : " + str(test_str))


Output

The original string is : gfg is best for geeks
String after reconstruction : GfG is BEst for GEEKs

Time complexity: O(nm), where n is the length of the input string and m is the number of custom characters to be replaced.
Auxiliary space: O(n), where n is the length of the input string. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads