One last question: how do you pass complex strings to functions in native Python?
💡 Model Answer
In Python, strings are immutable objects. When you pass a string to a function, the function receives a reference to the same string object, not a copy. Because the string cannot be modified in place, any "change" you make inside the function actually creates a new string and rebinds the local variable. For example:
def append_exclamation(s):
s += '!' # creates a new string
return s
original = "hello"
new = append_exclamation(original)
print(original) # still "hello"
print(new) # "hello!"If you need to modify a string-like object in place, use a mutable container such as a list of characters or a bytearray. You can also return the modified string and reassign it at the call site. For very large strings, consider streaming or chunking to avoid high memory usage. In summary, pass the string as an argument; the function will receive a reference, but any modifications will produce a new string unless you use a mutable type.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500