Python Program to Capitalize repeated characters in a string


In this article, we will learn how to capitalize repeated characters in a string in python.

Methods Used

The following are the various methods to accomplish this task -

  • Using Dictionary Hashing

  • Using count() Function

  • Using replace() and len() functions

  • Using Counter() function

Example

Assume we have taken an input string containing some random text. We will now convert the repeated characters in an input string into uppercase using the above methods.

Input

inputString = 'hello tutorialspoint'

Output

heLLO TuTOrIaLspOInT

In the above input string, the characters l, o, t, i are repeated. Hence they are converted into uppercase/capitalized.

Method 1: Using Dictionary Hashing

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task -.

  • Create a function RepeatedCharToUpper() that returns the repeated characters in a string to uppercase by accepting the input string as an argument.

  • Create an empty dictionary to store string characters frequencies.

  • Use the for loop to traverse through each character of an input string.

  • Use the if conditional statement to check whether the current character is already present in the above-created new dictionary.

  • Incrementing the frequency/count of character by 1, if the condition is true

  • Else add this character to the dictionary with a value of 1.

  • Again use the for loop to traverse through each character of an input string.

  • Use the if conditional statement to check whether the frequency of the current character is greater than 1(If count>1 then it is repeated).

  • Use the upper() function to change the character into uppercase.

  • Add that current character to the resultant string.

  • Return the resultant string.

  • Create a variable to store the input string.

  • Call the above-defined RepeatedCharToUpper() function by passing the input string to it and print the result.

Example

The following program returns a string after converting all the repeated characters in a string into uppercase using dictionary hashing -

# function to change the repeated characters in a string to uppercase

# by accepting the input string as an argument
def RepeatedCharToUpper(inputString):

   # Creating an empty dictionary to store characters with their frequencies
   newDict = {}
   
   # traversing through each character of an input string
   for c in inputString:
   
      # checking whether the character is present in the above dictionary
      if c in newDict:
      
      # Incrementing the frequency/count of character by 1
         newDict[c] = newDict[c]+1
      
      # Else insert this character in the dictionary
      else:
         newDict[c] = 1
   
   # Taking a variable to store the string
   res = ''
   
   # traversing through each character of an input string
   for c in inputString:
   
      # checking if character frequency is greater than 1(repeated character)
      if newDict[c] > 1:
      
         # As it is repeated so changing this character into uppercase
         c = c.upper()
      
      # adding each character to the resultant string
      res = res+c
      
   # returning the resultant string
   return res
   
# input string
inputString = 'hello tutorialspoint'
   
# calling the above defined RepeatedCharToUpper() function
# by passing input string to it
print(RepeatedCharToUpper(inputString))

Output

On executing, the above program will generate the following output -

heLLO TuTOrIaLspOInT

Method 2: Using count() Function

String count() function

Returns the no. of times the given value(char) appears in a string.

Syntax

string.count(value, start, end)

Example

The following program returns a string after converting all the repeated characters in a string into uppercase using the count() function -

# input string
inputString = 'hello tutorialspoint'

# empty string for storing resultant string
res = ""

# traversing through each character of an input string
for c in inputString:
   
   # checking whether the current character is not space and # its frequency is greater than 1
   if(c != "" and inputString.count(c) > 1):
      
      # converting to uppercase if the condition
      
      # and adding it to the resultant string
      res += c.upper()
   else:
      
      # else adding that current character to the resultant string without modifying
      res += c
print("Resultant string after capitalizing repeated characters:\n", res)

Output

On executing, the above program will generate the following output -

Resultant string after capitalizing repeated characters:
heLLO TuTOrIaLspOInT

Method 3: Using replace() and len() functions

len() function − The number of items in an object is returned by the len() method. The len() function returns the number of characters in a string when the object is a string.

replace() function − returns a copy of the string that replaces all occurrences of an old substring with another new substring.

Syntax

string.replace(old, new, count)

Example

The following program returns a string after converting all the repeated characters in a string into uppercase using replace() and len() functions -

# input string
inputString = 'hello tutorialspoint'

# getting string length
stringLength = len(inputString)

# empty string for storing resultant string
res = ""

# traversing through each character of an input string
for c in inputString:
   
   # replacing the current character with space
   k = inputString.replace(c, "")
   if(len(k) != stringLength-1):
      res += c.upper()
   else:
      res += c
print("Resultant string after capitalizing repeated characters:\n", res)

Output

On executing, the above program will generate the following output –

Resultant string after capitalizing repeated characters:
heLLO TuTOrIaLspOInT

Method 4: Using Counter() function

Counter() function − a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.

Here it returns the frequency of characters of string as key-value pairs.

Example

The following program returns a string after converting all the repeated characters in a string into uppercase using the Counter() function -

# importing Counter from the collections module
from collections import Counter

# input string
inputString = 'hello tutorialspoint'

# empty string for storing resultant string
res = ""

# getting the frequency of characters as a dictionary
frequency = Counter(inputString)

# traversing through each character of an input string
for c in inputString:
   
   # checking whether the current character is not space and its frequency is greater than 1
   if(c != "" and frequency[c] > 1):
      res += c.upper()
   else:
      res += c
print("Resultant string after capitalizing repeated characters:\n", res)

Output

Resultant string after capitalizing repeated characters:
heLLO TuTOrIaLspOInT

Conclusion

In this post, we learned 4 distinct methods for capitalizing repeated characters in a string. We also discovered how to get the frequencies of any iterable using dictionary hashing.

Updated on: 27-Jan-2023

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements