Tom Anonymous
Tom Anonymous

Reputation: 173

How do I convert only specific parts of a string to uppercase in Python?

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:

Upvotes: 5

Views: 6916

Answers (2)

Ryutlis
Ryutlis

Reputation: 301

def foo(str1, str2):
    return str1.replace(str2, str2.upper())

Upvotes: 20

Tim Pietzcker
Tim Pietzcker

Reputation: 336078

>>> a = "abcdefg"
>>> b = "cde"
>>> c = b.upper()
>>> a.replace(b,c)
'abCDEfg'

Upvotes: 6

Related Questions