How to update strings in a list based on condition
问题 I am stuck in a situation where I have a list and I want to change subnet /21 to /24 . x = ['192.168.8.1/21', '192.168.4.36/24', '192.168.47.54/16'] for a in x: if "21" in (a[-2:]): print(a) else: print("carry on") Now it's printing right values but how I can change the values of a[-2:] 21 to 24 I fail to understand. Output: 192.168.8.1/21 carry on carry on 回答1: You cannot change a part of a string as strings are immutable. But you can replace the string with a corrected version. x = ['192