-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment5_PythonMutationsProblem.py
More file actions
50 lines (40 loc) · 1.58 KB
/
Assignment5_PythonMutationsProblem.py
File metadata and controls
50 lines (40 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'''
Read a given string, change the character at a given index and then print the modified string.
Function Description
Complete the mutate_string function in the editor below.
mutate_string has the following parameters:
string string: the string to change
int position: the index to insert the character at
string character: the character to insert
Returns
string: the altered string
'''
def modify_string_using_list(s, index, new_char):
# Convert the string to a list
s_list = list(s)
# Change the character at the given index
s_list[index] = new_char
# Join the list back to a string
modified_string = ''.join(s_list)
return modified_string
def modify_string_using_slice(s, index, new_char):
# Slice the string and join it back after modification
modified_string = s[:index] + new_char + s[index+1:]
return modified_string
input_string = input("Enter the string: ")
index = int(input("Enter the index to modify: "))
new_char = input("Enter the new character: ")
# Method 1: Modify string using list
modified_string_list = modify_string_using_list(input_string, index, new_char)
print("Modified string using list method:", modified_string_list)
# Method 2: Modify string using slice
modified_string_slice = modify_string_using_slice(input_string, index, new_char)
print("Modified string using slice method:", modified_string_slice)
'''
Sample output :-
Enter the string: anjali
Enter the index to modify: 3
Enter the new character: L
Modified string using list method: anjLli
Modified string using slice method: anjLli
'''