So I am writing a function that takes in a string input (ex: abcdefg
) and a shorter portion of that input (ex: cde
) and searches for it within the first longer string.
How do I make it so that only that second portion is capitalized in the first string?
Ex:
Performing the . upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.
To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it. Now, we would get the remaining characters of the string using the slice() function.
def foo(str1, str2):
return str1.replace(str2, str2.upper())
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