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")
## [1] "Hello World, my name is Nicole."
As a vector:
params = c("World", "Nicole")
pystr_format("Hello {1}, my name is {2}.", params)
## [1] "Hello World, my name is Nicole."
Or as a list:
params = list("World", "Nicole")
pystr_format("Hello {1}, my name is {2}.", params)
## [1] "Hello World, my name is Nicole."
You can pass in named parameters individually:
pystr_format("Hello {thing}, my name is {name}.", thing="World", name="Nicole")
## [1] "Hello World, my name is Nicole."
As a named vector:
params = c(thing="World", name="Nicole")
pystr_format("Hello {thing}, my name is {name}.", params)
## [1] "Hello World, my name is Nicole."
Or as a named list:
params = list(thing="World", name="Nicole")
pystr_format("Hello {thing}, my name is {name}.", params)
## [1] "Hello World, my name is Nicole."
Parameters can be used more than once throughout a string:
pystr_format("The name is {last}. {first} {last}.", last="Bond", first="James")
## [1] "The name is Bond. James Bond."
Pass in characters and numbers:
pystr_format("Hello {name}, you have {n} new notifications!", name="Nicole", n=3)
## [1] "Hello Nicole, you have 3 new notifications!"
Port of str.capitalize.
pystr_capitalize("ONCE UPON A TIME, ")
## [1] "Once upon a time, "
Port of str.center.
pystr_center("center me", 15, "*")
## [1] "***center me***"
Port of str.count.
pystr_count("abcxyzabc123", "abc")
## [1] 2
Port of str.endswith.
pystr_endswith("selfie.jpg", ".jpg")
## [1] TRUE
pystr_endswith("selfie.jpg", c(".jpg", ".png"))
## [1] TRUE
Port of str.find.
pystr_find("12345", "34")
## [1] 3
Port of str.index.
pystr_index("12345", "34")
## [1] 3
pystr_index("12345", "xy")
## Error in pystr_index_(x, sub, start = start, end = end): ValueError
Port of str.isalnum.
pystr_isalnum("abc123")
## [1] TRUE
pystr_isalnum("abc123!")
## [1] FALSE
Port of str.isalpha.
pystr_isalpha("abc")
## [1] TRUE
pystr_isalpha("abc!")
## [1] FALSE
Port of str.islower.
pystr_islower("all lowercase!")
## [1] TRUE
pystr_islower("All lowercase?")
## [1] FALSE
Port of str.isnumeric.
pystr_isnumeric("123")
## [1] TRUE
pystr_isnumeric("123!")
## [1] FALSE
Port of str.isspace.
pystr_isspace(" ")
## [1] TRUE
Port of str.istitle.
pystr_istitle("I Am A Title")
## [1] TRUE
pystr_istitle("I Am not A Title")
## [1] FALSE
Port of str.isupper.
pystr_isupper("HELLO!")
## [1] TRUE
Port of str.join.
pystr_join(c("A", "BB", "CCC"))
## [1] "ABBCCC"
pystr_join(c(1, 2, 3), "+")
## [1] "1+2+3"
Port of str.ljust.
pystr_ljust("left", 10)
## [1] "left "
pystr_ljust("left", 10, "*")
## [1] "left******"
Port of str.lower.
pystr_lower("LOWERCASE ME!")
## [1] "lowercase me!"
Port of str.lstrip.
pystr_lstrip("www.example.com", "w.")
## [1] "example.com"
Port of str.maketrans.
map = pystr_maketrans("abc", "123")
pystr_translate("a blue cat", map)
## [1] "1 2lue 31t"
Port of str.partition.
pystr_partition("onetwothreeonetwothree", "two")
## [[1]]
## [1] "one" "two" "threeonetwothree"
Port of str.replace.
pystr_replace("mississippi", "ss", "X")
## [1] "miXiXippi"
pystr_replace("mississippi", "ss", "X", 1)
## [1] "miXissippi"
Port of str.rfind.
pystr_rfind("abcxyzabc", "abc")
## [1] 7
Port of str.rindex.
pystr_rindex("abcdab", "ab")
## [1] 5
pystr_rindex("abcdab", "xy")
## Error in pystr_rindex("abcdab", "xy"): ValueError
Port of str.rjust.
pystr_rjust("right", 10)
## [1] " right"
pystr_rjust("right", 10, "*")
## [1] "*****right"
Port of str.rpartition.
pystr_rpartition("onetwothreeonetwothree", "two")
## [[1]]
## [1] "onetwothreeone" "two" "three"
Port of str.rsplit.
pystr_rsplit("a--b--c", "--", 1)
## [[1]]
## [1] "a--b" "c"
Port of str.rstrip.
pystr_rstrip("mississippi", "ipz")
## [1] "mississ"
Port of str.split.
pystr_split("1+2+3+4", "+")
## [[1]]
## [1] "1" "2" "3" "4"
pystr_split("1+2+3+4", "+", 1)
## [[1]]
## [1] "1" "2+3+4"
Port of str.splitlines.
pystr_splitlines("First\nSecond\nThird")
## [[1]]
## [1] "First" "Second" "Third"
Port of str.startswith.
pystr_startswith("http://www.example.com", "http://")
## [1] TRUE
pystr_startswith("http://www.example.com", c("http://", "https://"))
## [1] TRUE
Port of str.strip.
pystr_strip(" very spacious ")
## [1] "very spacious"
pystr_strip("www.example.com", "wcom.")
## [1] "example"
Port of str.swapcase.
pystr_swapcase("Swap Me!")
## [1] "sWAP mE!"
Port of str.title.
pystr_title("make me pretty")
## [1] "Make Me Pretty"
Port of str.translate.
map = pystr_maketrans("abc", "123")
pystr_translate("a blue cat", map)
## [1] "1 2lue 31t"
Port of str.upper.
pystr_upper("uppercase me!")
## [1] "UPPERCASE ME!"
Port of str.zfill.
pystr_zfill("42", 5)
## [1] "00042"
pystr_zfill("-42", 5)
## [1] "-0042"