首页 > 解决方案 > Python replace method

问题描述

I write the code below but replace method is not working.

code:

courses = input("Please enter the courses you have taken previously with letter grades: ")
courses.replace("M","X")
print(courses)

Please enter the courses you have taken previously with letter grades:

MATH101:A;SPS101:B;CS201:B+;HIST191:D;CS204:F;CS210:S:
MATH101:A;SPS101:B;CS201:B+;HIST191:D;CS204:F;CS210:S:

标签: pythonmethodsreplace

解决方案


replace 不会在原地改变字符串。str.replace()

courses = courses.replace("M", "X")

推荐阅读