# Numeric placeholders
pystr_format("Hello {1}, my name is {2}.", "World", "Nicole")
pystr_format("Hello {1}, my name is {2}.", c("World", "Nicole"))
pystr_format("Hello {1}, my name is {2}.", list("World", "Nicole"))
# Named placeholders
pystr_format("Hello {thing}, my name is {name}.", thing="World", name="Nicole")
pystr_format("Hello {thing}, my name is {name}.", c(thing="World", name="Nicole"))
pystr_format("Hello {thing}, my name is {name}.", list(thing="World", name="Nicole"))
# Pass in characters and numbers
pystr_format("Hello {name}, you have {n} new notifications!", name="Nicole", n=2)
## Placeholders can be used more than once
pystr_format("The name is {last}. {first} {last}.", last="Bond", first="James")
## Pass in a whole data frame, matching by column names
my_cars <- data.frame(car=rownames(mtcars), mtcars)
head(pystr_format("The {car} gets {mpg} mpg (hwy) despite having {cyl} cylinders.", my_cars))
supers <- data.frame(first=c("Bruce", "Hal", "Clark", "Diana"),
last=c("Wayne", "Jordan", "Kent", "Prince"),
is=c("Batman", "Green Lantern", "Superman", "Wonder Woman"))
pystr_format("{first} {last} is really {is} but you shouldn't call them {first} in public.", supers)
Run the code above in your browser using DataLab