如何用Python编写将句子中的某个字母进行大小写变换?

2025-05-04 23:52:09
推荐回答(2个)
回答1:

# 如何用Python编写将句子中的某个字母进行大小写变换?
# sentence = 'i am iran man'
# word = 'i'
# 结果 upper_lower_word('i am iran man','i') => I am iran man

def upper_lower_word(sentence, word, is_upper = True):
index = sentence.find(word)
if index != -1:
if is_upper == True:
res_word = word.upper()
else:
res_word = word.lower()
res = sentence[0:index] + res_word + sentence[index+1:]
return res

回答2:

s.upper()