Related Articles

# Python – Capitalize repeated characters in a string

• Last Updated : 09 Jul, 2021

Given an input string with lowercase letters, the task is to write a python program to identify the repeated characters in the string and capitalize them.

Examples:

Input: programming language

Output: pRoGRAMMiNG lANGuAGe

Explanation: r,m,n,a,g are repeated elements

Input: geeks for geeks

Output: GEEKS for GEEKS

Explanation: g,e,k,s are repeated elements

Approach:

• We have to keep the character of a string as a key and the frequency of each character of the string as a value in the dictionary.
• Traverse the string and check the frequency of each character using a dictionary if the frequency of the character is greater than one then change the character to the uppercase using the upper() function.

Implementation:

## Python3

 `# function for changing the``# repeated characters to uppercase``def` `RepeatedUpper(s):` `    ``# declaring dictionary``    ``dic ``=` `{}` `    ``# Traversing the string``    ``for` `i ``in` `s:` `        ``# If the character is already``        ``# present in dictionary then increment``        ``# the frequency of the character``        ``if` `i ``in` `dic:``            ``dic[i] ``=` `dic[i]``+``1` `   ``# If the character is not present in``   ``# the dictionary then inserting``   ``# the character in the dictionary``        ``else``:``            ``dic[i] ``=` `1``    ``ans ``=` `''` `    ``# traversing the string``    ``for` `i ``in` `s:` `        ``# if the frequency of the character is``        ``# greater than one``        ``if` `dic[i] > ``1``:` `            ``# change into uppercase``            ``i ``=` `i.upper()` `        ``# appending each character to the ans``        ``ans ``=` `ans``+``i``    ``return` `ans`  `# Driver code``s ``=` `'geeks for geeks'``# fuction call``print``(RepeatedUpper(s))`

Output:

`GEEKS for GEEKS`

Time Complexity: O(n)

Space Complexity: O(n)

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

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.7719921,"math_prob":0.7578769,"size":1873,"snap":"2021-31-2021-39","text_gpt3_token_len":457,"char_repetition_ratio":0.1540931,"word_repetition_ratio":0.05362776,"special_character_ratio":0.22317138,"punctuation_ratio":0.10355029,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9559737,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-30T01:40:10Z\",\"WARC-Record-ID\":\"<urn:uuid:5899cfe8-6aaa-45b6-a541-a7c0b197037c>\",\"Content-Length\":\"107692\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:71403538-d630-4ed0-b4e3-6375b9b9b191>\",\"WARC-Concurrent-To\":\"<urn:uuid:140291f8-1992-47fd-9f09-da4fda38ec6a>\",\"WARC-IP-Address\":\"23.218.218.87\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-capitalize-repeated-characters-in-a-string/\",\"WARC-Payload-Digest\":\"sha1:A3SHGPAXDV3OKA43LHUX53M7RE7I4CKJ\",\"WARC-Block-Digest\":\"sha1:DG2H4LKTKK554XB22YJLEZYQTNZCL4WJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153899.14_warc_CC-MAIN-20210729234313-20210730024313-00373.warc.gz\"}"}