Reputation: 1086
I have a string like so:
"initWithType:bundleIdentifier:uniqueIdentifier:"
and two lists like so:
['long long', 'id', 'id']
['arg1', 'arg2', 'arg3']
and want to end up with the string:
"initWithType:(long long)arg1 bundleIdentifier:(id)arg2 uniqueIdentifier:(id)arg3"
As you may see, I effectively need to replace every nth semicolon with the nth string in each list (plus a little formatting with parentheses and a space).
I have been trying to use .format
and the *
unpacking operator but have had little success.
Upvotes: 1
Views: 49
Reputation: 59219
You can use a combination of string formatting with zip
:
s1 = "initWithType:bundleIdentifier:uniqueIdentifier:"
l2 = ['long long', 'id', 'id']
l3 = ['arg1', 'arg2', 'arg3']
print(" ".join("{}:({}){}".format(a, b, c) for a, b, c in zip(s1.split(":"), l2, l3)))
Edit: you can also use f-strings with Python >= 3.6 as suggested by @flakes:
print(" ".join(f"{a}:({b}){c}" for a, b, c in zip(s1.split(":"), l2, l3)))
this will print
initWithType:(long long)arg1 bundleIdentifier:(id)arg2 uniqueIdentifier:(id)arg3
Upvotes: 6