Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting specific letters to uppercase or lowercase in python

So to return a copy of a string converted to lowercase or uppercase one obviously uses the lower() or upper().

But how does one go about making a copy of a string with specific letters converted to upper or lowercase. For example how would i convert 'test' into 'TesT'

this is honestly baffling me so help is greatly appreciated

got it, thanks for the help Cyber and Matt!

like image 528
Muffen Avatar asked Oct 24 '25 14:10

Muffen


2 Answers

If you're just looking to replace specific letters:

>>> s = "test"
>>> s.replace("t", "T")
'TesT'
like image 184
MattDMo Avatar answered Oct 26 '25 04:10

MattDMo


There is one obvious solution, slice the string and upper the parts you want:

test = 'test'
test = test[0].upper() + test[1:-1] + test[-1].upper()
like image 34
Daniel Avatar answered Oct 26 '25 03:10

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!