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_capitalize
Port of str.capitalize.
pystr_capitalize("ONCE UPON A TIME, ")
pystr_center
Port of str.center.
pystr_center("center me", 15, "*")
pystr_count
Port of str.count.
pystr_count("abcxyzabc123", "abc")
pystr_endswith
Port of str.endswith.
pystr_endswith("selfie.jpg", ".jpg") pystr_endswith("selfie.jpg", c(".jpg", ".png"))
pystr_find
Port of str.find.
pystr_find("12345", "34")
pystr_index
Port of str.index.
pystr_index("12345", "34") pystr_index("12345", "xy")
pystr_isalnum
Port of str.isalnum.
pystr_isalnum("abc123") pystr_isalnum("abc123!")
pystr_isalpha
Port of str.isalpha.
pystr_isalpha("abc") pystr_isalpha("abc!")
pystr_islower
Port of str.islower.
pystr_islower("all lowercase!") pystr_islower("All lowercase?")
pystr_isnumeric
Port of str.isnumeric.
pystr_isnumeric("123") pystr_isnumeric("123!")
pystr_isspace
Port of str.isspace.
pystr_isspace(" ")
pystr_istitle
Port of str.istitle.
pystr_istitle("I Am A Title") pystr_istitle("I Am not A Title")
pystr_isupper
Port of str.isupper.
pystr_isupper("HELLO!")
pystr_join
Port of str.join.
pystr_join(c("A", "BB", "CCC")) pystr_join(c(1, 2, 3), "+")
pystr_ljust
Port of str.ljust.
pystr_ljust("left", 10) pystr_ljust("left", 10, "*")
pystr_lower
Port of str.lower.
pystr_lower("LOWERCASE ME!")
pystr_lstrip
Port of str.lstrip.
pystr_lstrip("www.example.com", "w.")
pystr_maketrans
Port of str.maketrans.
map = pystr_maketrans("abc", "123") pystr_translate("a blue cat", map)
pystr_partition
Port of str.partition.
pystr_partition("onetwothreeonetwothree", "two")
pystr_replace
Port of str.replace.
pystr_replace("mississippi", "ss", "X") pystr_replace("mississippi", "ss", "X", 1)
pystr_rfind
Port of str.rfind.
pystr_rfind("abcxyzabc", "abc")
pystr_rindex
Port of str.rindex.
pystr_rindex("abcdab", "ab") pystr_rindex("abcdab", "xy")
pystr_rjust
Port of str.rjust.
pystr_rjust("right", 10) pystr_rjust("right", 10, "*")
pystr_rpartition
Port of str.rpartition.
pystr_rpartition("onetwothreeonetwothree", "two")
pystr_rsplit
Port of str.rsplit.
pystr_rsplit("a--b--c", "--", 1)
pystr_rstrip
Port of str.rstrip.
pystr_rstrip("mississippi", "ipz")
pystr_split
Port of str.split.
pystr_split("1+2+3+4", "+") pystr_split("1+2+3+4", "+", 1)
pystr_splitlines
Port of str.splitlines.
pystr_splitlines("First\nSecond\nThird")
pystr_startswith
Port of str.startswith.
pystr_startswith("http://www.example.com", "http://") pystr_startswith("http://www.example.com", c("http://", "https://"))
pystr_strip
Port of str.strip.
pystr_strip(" very spacious ") pystr_strip("www.example.com", "wcom.")
pystr_swapcase
Port of str.swapcase.
pystr_swapcase("Swap Me!")
pystr_title
Port of str.title.
pystr_title("make me pretty")
pystr_translate
Port of str.translate.
map = pystr_maketrans("abc", "123") pystr_translate("a blue cat", map)
pystr_upper
Port of str.upper.
pystr_upper("uppercase me!")
pystr_zfill
Port 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.