2

I want to replace every second occurence of a character from only a string as input. Using only the .replace method.

For example:

input: asdasdasdasd

output should be: asdASDasdasd

def main(string):
   for char in string:
      string.replace(char, char.upper())
   return string

Im relatively new to Python and I can't wrap my head around what to do.

6
  • replace returns a new string Commented May 10, 2022 at 11:50
  • All right, but then I can make a new string y and use that. But how can I replace only the second occurence of the character? Commented May 10, 2022 at 11:52
  • 1
    If input_1="aaa", what is the expected output_1? If input_2="aaaaaa", what is the expected output_2? Commented May 10, 2022 at 11:53
  • 1
    output_1 = 'aAa'. Output_2 = 'aAaaaa' Commented May 10, 2022 at 11:54
  • @Mart if `input='AAA', Then what? Commented May 10, 2022 at 11:58

3 Answers 3

2

Alternatively, you can use a dictionary and make the second occurrence of each character to uppercase:

def upper_case_second_occurrence(s):
    d = {}
    s = list(s)
    for i, c in enumerate(s):
        d[c] = d.get(c, 0) + 1
        if d[c] == 2:
            s[i] = c.upper()
    return "".join(s)


if __name__ == "__main__":
    print(upper_case_second_occurrence("aaa"))
    print(upper_case_second_occurrence("aaaaaa"))
    print(upper_case_second_occurrence("asdasdasdasd"))

Output:

aAa
aAaaaa
asdASDasdasd
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Do you also have an answer when I can only use the .replace method for example?
The doc for str.replace mentions it replaces all or the first n occurrences of a character from a string with a new character docs.python.org/3/library/stdtypes.html#str.replace . I do not think you can only use replace method in your scenario.
1

Try this one:

def main(s):
    l = []
    string = ''
    y = True
    for a in s:
        if a in l and y:
            string+=a.upper()
            l.remove(a)
            if not len(l):
                l = ''
            continue
        try:
            l.append(a)
        except AttributeError:
            y = False
        string+=a
    return(string)

print(main('asdasdasdasd')) # → asdASDasdasd
print(main('aaa')) # → aAa
print(main('aaaaa')) # → aAaaa
print(main('AAA') # → AAA

Comments

0

Try using .count():

def main(string):
    new_string = ''
    for char in string:
        if new_string.lower().count(char) == 1: # This line checks if the count of char is 1 or not. If yes, then it appends the uppercase letter of char
            new_string += char.upper()
        else:
            new_string += char
    return new_string

Output:

main('asdasdasdasd')
'asdASDasdasd'
main('aaaaaa')
'aAaaaa'
main('aaa')
'aAa'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.