Search code examples
python-3.xstringmethodsreplaceuppercase

python string first and last char identification problem


if first and last char of a string in python are the same while using replace method and uppercase on only the first char the change happens on both first and last char , why??

>>> text = 'standards'
>>> text = text.replace(text[0],(text[0].upper()))
>>> text
'StandardS'

Solution

  • .replace() function will replace all characters in string, if you just want to change first char,you can trying divide string and conquer after, like this:

    text=f'{text[0].upper()}{text[1:]}'