pystr
pystr copied to clipboard
Python String Methods in R.
pystr

String operations the Python way: a package for those of us who miss Python's string methods while we're working in R.
Install
From CRAN (Stable Release)
install.packages("pystr")
From GitHub (Latest Development Release)
devtools::install_github("nicolewhite/pystr")
Load the Package
library(pystr)
String Formatting
Most importantly, pystr
brings a port of Python's str.format to R with pystr_format
.
Indexed Parameters
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."
Named Parameters
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."
Repeated Parameters
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."
Replacement Types
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!"
Other Ported Methods
pystr_capitalize
Port of str.capitalize.
pystr_capitalize("ONCE UPON A TIME, ")
## [1] "Once upon a time, "
pystr_center
Port of str.center.
pystr_center("center me", 15, "*")
## [1] "***center me***"
pystr_count
Port of str.count.
pystr_count("abcxyzabc123", "abc")
## [1] 2
pystr_endswith
Port of str.endswith.
pystr_endswith("selfie.jpg", ".jpg")
## [1] TRUE
pystr_endswith("selfie.jpg", c(".jpg", ".png"))
## [1] TRUE
pystr_find
Port of str.find.
pystr_find("12345", "34")
## [1] 3
pystr_index
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
pystr_isalnum
Port of str.isalnum.
pystr_isalnum("abc123")
## [1] TRUE
pystr_isalnum("abc123!")
## [1] FALSE
pystr_isalpha
Port of str.isalpha.
pystr_isalpha("abc")
## [1] TRUE
pystr_isalpha("abc!")
## [1] FALSE
pystr_islower
Port of str.islower.
pystr_islower("all lowercase!")
## [1] TRUE
pystr_islower("All lowercase?")
## [1] FALSE
pystr_isnumeric
Port of str.isnumeric.
pystr_isnumeric("123")
## [1] TRUE
pystr_isnumeric("123!")
## [1] FALSE
pystr_isspace
Port of str.isspace.
pystr_isspace(" ")
## [1] TRUE
pystr_istitle
Port of str.istitle.
pystr_istitle("I Am A Title")
## [1] TRUE
pystr_istitle("I Am not A Title")
## [1] FALSE
pystr_isupper
Port of str.isupper.
pystr_isupper("HELLO!")
## [1] TRUE
pystr_join
Port of str.join.
pystr_join(c("A", "BB", "CCC"))
## [1] "ABBCCC"
pystr_join(c(1, 2, 3), "+")
## [1] "1+2+3"
pystr_ljust
Port of str.ljust.
pystr_ljust("left", 10)
## [1] "left "
pystr_ljust("left", 10, "*")
## [1] "left******"
pystr_lower
Port of str.lower.
pystr_lower("LOWERCASE ME!")
## [1] "lowercase me!"
pystr_lstrip
Port of str.lstrip.
pystr_lstrip("www.example.com", "w.")
## [1] "example.com"
pystr_maketrans
Port of str.maketrans.
map = pystr_maketrans("abc", "123")
pystr_translate("a blue cat", map)
## [1] "1 2lue 31t"
pystr_partition
Port of str.partition.
pystr_partition("onetwothreeonetwothree", "two")
## [[1]]
## [1] "one" "two" "threeonetwothree"
pystr_replace
Port of str.replace.
pystr_replace("mississippi", "ss", "X")
## [1] "miXiXippi"
pystr_replace("mississippi", "ss", "X", 1)
## [1] "miXissippi"
pystr_rfind
Port of str.rfind.
pystr_rfind("abcxyzabc", "abc")
## [1] 7
pystr_rindex
Port of str.rindex.
pystr_rindex("abcdab", "ab")
## [1] 5
pystr_rindex("abcdab", "xy")
## Error in pystr_rindex("abcdab", "xy"): ValueError
pystr_rjust
Port of str.rjust.
pystr_rjust("right", 10)
## [1] " right"
pystr_rjust("right", 10, "*")
## [1] "*****right"
pystr_rpartition
Port of str.rpartition.
pystr_rpartition("onetwothreeonetwothree", "two")
## [[1]]
## [1] "onetwothreeone" "two" "three"
pystr_rsplit
Port of str.rsplit.
pystr_rsplit("a--b--c", "--", 1)
## [[1]]
## [1] "a--b" "c"
pystr_rstrip
Port of str.rstrip.
pystr_rstrip("mississippi", "ipz")
## [1] "mississ"
pystr_split
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"
pystr_splitlines
Port of str.splitlines.
pystr_splitlines("First\nSecond\nThird")
## [[1]]
## [1] "First" "Second" "Third"
pystr_startswith
Port of str.startswith.
pystr_startswith("http://www.example.com", "http://")
## [1] TRUE
pystr_startswith("http://www.example.com", c("http://", "https://"))
## [1] TRUE
pystr_strip
Port of str.strip.
pystr_strip(" very spacious ")
## [1] "very spacious"
pystr_strip("www.example.com", "wcom.")
## [1] "example"
pystr_swapcase
Port of str.swapcase.
pystr_swapcase("Swap Me!")
## [1] "sWAP mE!"
pystr_title
Port of str.title.
pystr_title("make me pretty")
## [1] "Make Me Pretty"
pystr_translate
Port of str.translate.
map = pystr_maketrans("abc", "123")
pystr_translate("a blue cat", map)
## [1] "1 2lue 31t"
pystr_upper
Port of str.upper.
pystr_upper("uppercase me!")
## [1] "UPPERCASE ME!"
pystr_zfill
Port of str.zfill.
pystr_zfill("42", 5)
## [1] "00042"
pystr_zfill("-42", 5)
## [1] "-0042"