String operations the Python way: a package for those of us who miss Python's string methods while we're working in R.
install.packages("pystr")
devtools::install_github("nicolewhite/pystr")
library(pystr)
Most importantly, pystr brings a port of Python's str.format to R with pystr_format.
You can pass in parameters individually:
pystr_format("Hello {1}, my name is {2}.", "World", "Nicole")
As a vector:
params = c("World", "Nicole") pystr_format("Hello {1}, my name is {2}.", params)
Or as a list:
params = list("World", "Nicole") pystr_format("Hello {1}, my name is {2}.", params)
You can pass in named parameters individually:
pystr_format("Hello {thing}, my name is {name}.", thing="World", name="Nicole")
As a named vector:
params = c(thing="World", name="Nicole") pystr_format("Hello {thing}, my name is {name}.", params)
Or as a named list:
params = list(thing="World", name="Nicole") pystr_format("Hello {thing}, my name is {name}.", params)
Parameters can be used more than once throughout a string:
pystr_format("The name is {last}. {first} {last}.", last="Bond", first="James")
Pass in characters and numbers:
pystr_format("Hello {name}, you have {n} new notifications!", name="Nicole", n=3)
pystr_capitalizePort of str.capitalize.
pystr_capitalize("ONCE UPON A TIME, ")
pystr_centerPort of str.center.
pystr_center("center me", 15, "*")
pystr_countPort of str.count.
pystr_count("abcxyzabc123", "abc")
pystr_endswithPort of str.endswith.
pystr_endswith("selfie.jpg", ".jpg") pystr_endswith("selfie.jpg", c(".jpg", ".png"))
pystr_findPort of str.find.
pystr_find("12345", "34")
pystr_indexPort of str.index.
pystr_index("12345", "34") pystr_index("12345", "xy")
pystr_isalnumPort of str.isalnum.
pystr_isalnum("abc123") pystr_isalnum("abc123!")
pystr_isalphaPort of str.isalpha.
pystr_isalpha("abc") pystr_isalpha("abc!")
pystr_islowerPort of str.islower.
pystr_islower("all lowercase!") pystr_islower("All lowercase?")
pystr_isnumericPort of str.isnumeric.
pystr_isnumeric("123") pystr_isnumeric("123!")
pystr_isspacePort of str.isspace.
pystr_isspace(" ")
pystr_istitlePort of str.istitle.
pystr_istitle("I Am A Title") pystr_istitle("I Am not A Title")
pystr_isupperPort of str.isupper.
pystr_isupper("HELLO!")
pystr_joinPort of str.join.
pystr_join(c("A", "BB", "CCC")) pystr_join(c(1, 2, 3), "+")
pystr_ljustPort of str.ljust.
pystr_ljust("left", 10) pystr_ljust("left", 10, "*")
pystr_lowerPort of str.lower.
pystr_lower("LOWERCASE ME!")
pystr_lstripPort of str.lstrip.
pystr_lstrip("www.example.com", "w.")
pystr_maketransPort of str.maketrans.
map = pystr_maketrans("abc", "123") pystr_translate("a blue cat", map)
pystr_partitionPort of str.partition.
pystr_partition("onetwothreeonetwothree", "two")
pystr_replacePort of str.replace.
pystr_replace("mississippi", "ss", "X") pystr_replace("mississippi", "ss", "X", 1)
pystr_rfindPort of str.rfind.
pystr_rfind("abcxyzabc", "abc")
pystr_rindexPort of str.rindex.
pystr_rindex("abcdab", "ab") pystr_rindex("abcdab", "xy")
pystr_rjustPort of str.rjust.
pystr_rjust("right", 10) pystr_rjust("right", 10, "*")
pystr_rpartitionPort of str.rpartition.
pystr_rpartition("onetwothreeonetwothree", "two")
pystr_rsplitPort of str.rsplit.
pystr_rsplit("a--b--c", "--", 1)
pystr_rstripPort of str.rstrip.
pystr_rstrip("mississippi", "ipz")
pystr_splitPort of str.split.
pystr_split("1+2+3+4", "+") pystr_split("1+2+3+4", "+", 1)
pystr_splitlinesPort of str.splitlines.
pystr_splitlines("First\nSecond\nThird")
pystr_startswithPort of str.startswith.
pystr_startswith("http://www.example.com", "http://") pystr_startswith("http://www.example.com", c("http://", "https://"))
pystr_stripPort of str.strip.
pystr_strip(" very spacious ") pystr_strip("www.example.com", "wcom.")
pystr_swapcasePort of str.swapcase.
pystr_swapcase("Swap Me!")
pystr_titlePort of str.title.
pystr_title("make me pretty")
pystr_translatePort of str.translate.
map = pystr_maketrans("abc", "123") pystr_translate("a blue cat", map)
pystr_upperPort of str.upper.
pystr_upper("uppercase me!")
pystr_zfillPort of str.zfill.
pystr_zfill("42", 5) pystr_zfill("-42", 5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.