Steffi Keran Rani J
Steffi Keran Rani J

Reputation: 4093

Error while Trying To Print the First Occurrence of a repeating Character in a String using Python 3.6

I am writing a simple program to replace the repeating characters in a string with an *(asterisk). But the thing here is I can print the 1st occurrence of a repeating character in a string, but not the other occurrences.

For example, if my input is Google, my output should be Go**le.

I am able to replace the characters that repeat with an asterisk, but just cant find a way to print the 1st occurrence of the character. In other words, my output right now is ****le.

Have a look at my Python3 code for this:

s = 'Google'
s = s.lower()
for i in s:
    if s.count(i)>1:
        s = s.replace(i,'*')
print(s)

Can someone suggest me what should be done to get the required output?

Upvotes: 4

Views: 411

Answers (3)

Bart Van Loon
Bart Van Loon

Reputation: 1510

How about using list comprensions? When constructing a list from another list (which is kind of what you are doing here, since we're considering strings as lists), list comprehension is a great tool:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

inputstring = 'Google'.lower()
outputstring = ''.join(
    [char if inputstring.find(char, 0, index) == -1 else '*'
    for index, char in enumerate(inputstring)])
print(outputstring)

This results in go**le.

Hope this helps!

(edited to use '*' as the replacement character instead of '#')

Upvotes: 2

Duc Filan
Duc Filan

Reputation: 7157

This is my approach:

First, you need to find the nth occurrence of the character. Then, you can replace other occurrences by using this snippet:

s = s[:position] + '*' + s[position+1:]

Full example code:

def find_nth(haystack, needle, n):
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start+len(needle))
        n -= 1
    return start

s = 'Google'
s_lower = s.lower()

for c in s_lower:
    if s_lower.count(c) > 1:
        position = find_nth(s_lower, c, 2)
        s = s[:position] + '*' + s[position+1:]
print(s)

Runnable link: https://repl.it/Mc4U/4


Regex approach:

import re

s = 'Google'

s_lower = s.lower()

for c in s_lower:
    if s_lower.count(c) > 1:
        position = [m.start() for m in re.finditer(c, s_lower)][1]
        s = s[:position] + '*' + s[position+1:]

print(s)

Runnable link: https://repl.it/Mc4U/3

Upvotes: 2

Chen A.
Chen A.

Reputation: 11280

replace will replace ALL occurences of the char. You need to follow on the characters you already have seen, and if they are repeated to replace JUST this character (at specific index).

Strings don't support index assignment, so we can build a new list that represents the new string and ''.join() it afterwards.

Using Set you can follow on what items you have seen already.

It would look like this:

s = 'Google'
seen = set()
new_string = []

for c in s:
    if c.lower() in seen:
        new_string.append('*')
    else:
        new_string.append(c)
        seen.add(c.lower())

new_string = ''.join(new_string)
print(new_string)
Go**le

Upvotes: 2

Related Questions