I need to create a function that takes the parameters (character, stringOne, stringTwo). The function returns a new word which contain the same data as stringTwo + whatever characters in stringOne in the same position.
Example:
I am lost on how I should proceed.
def replace(char, word1, word2):
newWord = ""
for s in range(len(word1)):
if word1[s] == char:
return
Continuing what you have done, using simple for-loop
and ternary operation, this is how it can be done:
def replace(char, word1, word2):
newWord = ""
for s in range(len(word1)):
newWord += char if word1[s] == char else word2[s]
return newWord
Which is equivalent to:
def replace(char, word1, word2):
newWord = ""
for s in range(len(word1)):
if word1[s] == char:
newWord += char
else:
newWord += word2[s]
return newWord
Just in case you are not familiar with ternary operation.
Here's how to modify your existing code so that it does what you want. I've changed newWord
to new_word
to conform with the standard Python style convention.
def replace(char, word1, word2):
new_word = ""
for i in range(len(word2)):
if word1[i] == char:
new_word += char
else:
new_word += word2[i]
return new_word
# Test
print(replace("p", "apple", "12345"))
output
1pp45
Note that this code will raise an IndexError: string index out of range
exception if word1
is shorter than word2
.
There are various better ways to implement this function. Firstly, we can use the zip
function to iterate over the characters of both strings in parallel. This is a bit cleaner than your indirect iteration using the indices of the strings to access the characters.
def replace(char, word1, word2):
new_word = ""
for c1, c2 in zip(word1, word2):
if c1 == char:
new_word += char
else:
new_word += c2
return new_word
Note that if the strings aren't the same length then zip
will stop gracefully when the shorter string finishes.
We can make that code more compact by replacing the if... else
block with a conditional expression.
def replace(char, word1, word2):
new_word = ""
for c1, c2 in zip(word1, word2):
new_word += char if c1 == char else c2
return new_word
A more advanced version uses a list comprehension to build a list of the desired characters and the string .join
method to convert that list into a string.
def replace(char, word1, word2):
return ''.join([char if c1 == char else c2
for c1, c2 in zip(word1, word2)])
It's also possible to do this using a generator expression.
''.join(char if c1 == char else c2 for c1, c2 in zip(word1, word2))
However that's less efficient than using a list comprehension, due to the way that .join
works. .join
has to scan its argument twice: the first scan is used to calculate the total size of the output string, the second scan does the actual joining. But you can't scan a generator twice, so if you pass .join
a generator it has to build a temporary list from the generator.
def replace(char, word1, word2):
newWord= list(word2)
for counter in range(min(len(word1), len(word2))):
if word1[counter]== char:
newWord[counter]= char
return "".join(newWord)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With