Suppose we have some string prefix and suffix that we want to reuse.
prefix = "My name is "
suffix = "Nice to meet you."
for name in ['A', 'B', 'C']:
print(prefix + name + suffix)
Is there any way to write the code above in a more succinct way, such that variable substitution can be done on an instance for a single string instance, similar to the f"{var}"
method, but with the string being an instance?
Python strings are immutable. Therefore you will have to create a new one each time. Sorry :( As for shorter code, you could use list comprehension or map to make it into a single line:
[prefix + name + suffix for name in ['A','B', 'C']]