python 字符串首末字符识别问题

python string first and last char identification problem

如果 python 中字符串的第一个字符和最后一个字符相同,而仅在第一个字符上使用替换方法和大写字母,那么第一个字符和最后一个字符都会发生变化,为什么??

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

.replace() 函数将替换字符串中的所有字符, 如果你只想改变第一个字符,你可以尝试划分字符串并在之后征服,像这样:

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