It's really simple code and I can try other ways but I really can't figure out what's specifically wrong with the logic of the code I've written.
def capitalize_letter(text, letter):
for x in text:
letter.upper()
return text
capitalize_letter('goku', 'O')
CodePudding user response:
Your code doesn't actually change the original text
variable at all, it just returns exactly what you fed in.
for x in text:
will iterate through each character in text, which you could use to test if that character matches the letter you want to capitalise, but then you do nothing with x, making it useless.
Then you write letter.upper()
, but you never update letter from the 'O' fed in, so this line will just return 'O' each loop, but you don't assign it to anything/do anything with it, so it's pretty useless.
A potential way to change your code would be:
I would use an empty string to store the results in, like so:
def capitalize_letter(text, letter):
output_text = ''
Then your for loop is correct, and you can append each character in the text (x) to the output text string:
def capitalize_letter(text, letter):
output_text = ''
for x in text:
output_text = x
return output_text
This will return output_text
which will match text
. However, next you need to check if each character (x) matches the letter you want to capitalise, and if so, make it a capital letter, like so:
if x == letter:
x = x.upper()
Put all together, you get:
def capitalize_letter(text, letter):
output_text = ''
for x in text:
if x == letter:
x = x.upper()
output_text = x
return output_text
NOTE when you call this function, you should ensure letter
is lower case, as you want to change from lowercase to uppercase, not uppercase to uppercase.
e.g:
capitalize_letter('goku', 'o')
'gOku'
CodePudding user response:
By using a loop you are iterating through all the letters of the text not just the desired letter. You need to check whether the x==letter and then make x.upper().
CodePudding user response:
You need to check the letter and then replace it in you current text.
def capitalize_letter(text, letter):
for x in text:
if(x==letter):
text=text.replace(letter,letter.upper());
return text
text = capitalize_letter('goku', 'o')
print(text)
CodePudding user response:
There are a couple of things:
You are looking for captial "O" in your input text. But your input text is all all lower-case.
You are iterating with the variable "x", but use "letter.upper()" instead of "x.upper()"
"x.upper()" only returns the letter in upper-case, hence you have to assign it before you can use it.
def capitalize_letter(text, letter):
output = ''
for x in text:
if x == letter:
x = x.upper()
output = x
return output
print(capitalize_letter('goku', 'o'))
Output is "gOku"
CodePudding user response:
In python, the string data types are immutable. Which means a string value cannot be updated. However in order to capitalise any letter you can create a shallow copy of the string, update accordingly and replace with the original one...
Here is the solution....
def capitalize_letter(text, letter):
text=text.replace(letter,letter.upper())
print(text)
capitalize_letter('hello', 'l')