0

I have copied the following code with some alteration in naming to solve the problem as stated: Given a string s, return a string where all occurences of its first char have been changed to '*', except do not change the first char itself. e.g. 'babble' yields 'ba**le'. Assume that the string is length 1 or more. Here is the code: My question is when I pass "Lulla" why don't I see "Lu**a" returned in the interpreter as I think the code should do.

def fix_start(s):
    start_letter = s[0]
    rest_letters = s[1:]
    rest_letters = rest_letters.replace(start_letter,'*')  
    return start_letter + rest_letters

print (fix_start ('Lulla'))
Steve Howard
  • 6,737
  • 1
  • 26
  • 37
user3806691
  • 55
  • 1
  • 8

4 Answers4

1

What happens is that Python is case-sensitive. In other words:

'L' != 'l'

So, when you do

rest_letters = rest_letters.replace(start_letter,'*')

it will replace all occurences of only L, not both L and l.

What can you do? The process of case-insensitive replacing is kind of complex, as you can see in these answers. But in your case, this may work:

rest_letters = rest_letters.replace(start_letter,'*').replace(start_letter.lower(), '*')
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

In your code, you are replacing any instance of the first letter "L" in the remainder of the string. In the case of the example, "Lulla", the first letter is a capital "L", and is not equal to "l". As such, I would try the following:

def fix_start(s):
    start_letter = s[0]
    rest_letters = s[1:]
    rest_letters = rest_letters.replace(start_letter.lower(),'*')
    return start_letter + rest_letters

The above solution will work if you have strings that are guaranteed to be syntactically correct (as in no uppercase letters in the middle of the word).

If that is not guaranteed, go ahead and try this:

def fix_start(s):
    start_letter = s[0]
    rest_letters = s[1:]
    rest_letters = rest_letters.replace(start_letter.lower(),'*')
    rest_letters = rest_letters.replace(start_letter, '*')
    return start_letter + rest_letters
siddk
  • 13
  • 4
0

Actually your trying to replace L but rest of the characters are small l.Try using regular expression that makes this task easy.

import re
def fix_start(s):
    start_letter = s[0]
    regex = s[0].lower()+"|"+s[0].upper()
    rest_letters = s[1:]
    rest_letters = re.sub(regex, "*", rest_letters) 
    return start_letter + rest_letters

print (fix_start ('Lulla'))
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46
0

Quite simply all that is required is to use lower() or upper() to change the string case:

start_letter = s[0].lower()
rest_letters = s[1:].lower()

Input: LulLa
Output: lu**a

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • I think this is an odd interpretation. For some reason I assumed the case of the first letter shouldn't be changed in the output. – John La Rooy Jul 08 '14 at 01:08
  • @gnibbler, Valid point; I interpreted the question/issue as how to match the word (regardless of case). The first character case could always be switched back after the match to retain that of the original `return s[0] + rest_letters`. – l'L'l Jul 13 '14 at 05:58