Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

string

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:

  • Input 1: abcdefg
  • Input 2: cde
  • abCDEfg
like image 850
Tom Anonymous Avatar asked Jul 22 '13 17:07

Tom Anonymous


People also ask

How do I make part of a string uppercase in Python?

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.

How do you capitalize certain letters in a string?

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.


1 Answers

def foo(str1, str2):
    return str1.replace(str2, str2.upper())
like image 102
Ryutlis Avatar answered Oct 04 '22 22:10

Ryutlis