1% Generated by roxygen2 (4.1.1): do not edit by hand
2% Please edit documentation in R/pystr_format.R
3\name{pystr_format}
4\alias{pystr_format}
5\title{Format a string.}
6\usage{
7pystr_format(str, ...)
8}
9\arguments{
10\item{str}{A character vector.}
11
12\item{...}{Parameter values. See details and examples}
13}
14\value{
15A character vector.
16}
17\description{
18Perform a string formatting operation.
19}
20\details{
21The string on which this method is called can contain
22literal text or replacement fields delimited by braces \code{\{\}}. Each replacement field contains
23either the numeric index of a positional argument, or the name of a keyword argument. Returns
24a copy of the string where each replacement field is replaced with the string value of the
25corresponding argument.
26
27If \code{...} is a single argument of a \code{data.frame}-like object, \code{pystr_format} will
28return an \code{nrow()}-length character vector using the column names of the data.frame for
29the named \code{\{placeholder\}}s.
30}
31\examples{
32# Numeric placeholders
33
34pystr_format("Hello {1}, my name is {2}.", "World", "Nicole")
35pystr_format("Hello {1}, my name is {2}.", c("World", "Nicole"))
36pystr_format("Hello {1}, my name is {2}.", list("World", "Nicole"))
37
38# Named placeholders
39
40pystr_format("Hello {thing}, my name is {name}.", thing="World", name="Nicole")
41pystr_format("Hello {thing}, my name is {name}.", c(thing="World", name="Nicole"))
42pystr_format("Hello {thing}, my name is {name}.", list(thing="World", name="Nicole"))
43
44# Pass in characters and numbers
45
46pystr_format("Hello {name}, you have {n} new notifications!", name="Nicole", n=2)
47
48## Placeholders can be used more than once
49
50pystr_format("The name is {last}. {first} {last}.", last="Bond", first="James")
51
52## Pass in a whole data frame, matching by column names
53
54my_cars <- data.frame(car=rownames(mtcars), mtcars)
55head(pystr_format("The {car} gets {mpg} mpg (hwy) despite having {cyl} cylinders.", my_cars))
56
57supers <- data.frame(first=c("Bruce", "Hal", "Clark", "Diana"),
58                     last=c("Wayne", "Jordan", "Kent", "Prince"),
59                     is=c("Batman", "Green Lantern", "Superman", "Wonder Woman"))
60pystr_format("{first} {last} is really {is} but you shouldn't call them {first} in public.", supers)
61}
62\references{
63\url{https://docs.python.org/3/library/stdtypes.html#str.format}
64}
65
66