Functions Examples! 💕¶
🚀 Extend your string powers with Str+
Setup [Optional]¶
Create a virtualenv 🔧
Create a new virtualenv before start this notebook to be able to select it as the kernel, if you want!- Create a new virtualenv.
pyenv virtualenv 3.9.16 .envStrPlus
pyenv activate .envStrPlus
pip install --upgrade pip
pip install ipykernel
Delete the virtualenv.
pyenv deactivate .envStrPlus pyenv virtualenv-delete -f .envStrPlus
Should return empty
pyenv versions | grep .envStrPlus
Required! 💢¶
In [ ]:
Copied!
pip install --upgrade strplus
pip install --upgrade strplus
In [10]:
Copied!
from strplus import Str
from strplus import Str
📜 Print inside!¶
In [11]:
Copied!
my_new_string = Str("Print me please, simple and easy!")
my_new_string.print
my_new_string = Str("Print me please, simple and easy!")
my_new_string.print
Print me please, simple and easy!
In [12]:
Copied!
for word in my_new_string.list:
word.print
for word in my_new_string.list:
word.print
Print me please simple and easy
🔂 Get most frequent separator¶
In [13]:
Copied!
from strplus.functions import get_separator
from strplus.functions import get_separator
In [14]:
Copied!
mystring = "test;separator;correct,one,test|new"
get_separator(mystring)
mystring = "test;separator;correct,one,test|new"
get_separator(mystring)
Out[14]:
','
In [15]:
Copied!
mystring = "test;separator;correct\none,test|new"
get_separator(mystring)
mystring = "test;separator;correct\none,test|new"
get_separator(mystring)
Out[15]:
';'
In [16]:
Copied!
mystring = "\ntest;\nseparator;correct\none,test|new"
get_separator(mystring)
mystring = "\ntest;\nseparator;correct\none,test|new"
get_separator(mystring)
Out[16]:
';'
In [17]:
Copied!
mystring = "col1;col2,col3"
get_separator(mystring)
mystring = "col1;col2,col3"
get_separator(mystring)
Out[17]:
','
In [18]:
Copied!
mystring = "col1;col2,col3;col4"
get_separator(mystring)
mystring = "col1;col2,col3;col4"
get_separator(mystring)
Out[18]:
';'
In [19]:
Copied!
input_string = "apple;banana,cherry"
result = get_separator(input_string)
print(result)
result == ","
input_string = "apple;banana,cherry"
result = get_separator(input_string)
print(result)
result == ","
,
Out[19]:
True
In [20]:
Copied!
input_string = "apple;banana,cherry"
result = get_separator(input_string, separator_list=[";",","])
print(result)
result == ";"
input_string = "apple;banana,cherry"
result = get_separator(input_string, separator_list=[";",","])
print(result)
result == ";"
;
Out[20]:
True
✂️ Function - Split by Separator¶
In [21]:
Copied!
from strplus.functions import split_by_separator
from strplus.functions import split_by_separator
In [22]:
Copied!
split_by_separator("one,two,three")
split_by_separator("one,two,three")
Out[22]:
['one', 'two', 'three']
In [23]:
Copied!
split_by_separator("one;two;three")
split_by_separator("one;two;three")
Out[23]:
['one', 'two', 'three']
In [24]:
Copied!
split_by_separator(" one\ttwo\tthree ")
split_by_separator(" one\ttwo\tthree ")
Out[24]:
['one', 'two', 'three']
The most frequent separator is ","
In [25]:
Copied!
split_by_separator(" one\ttwo\tthree,for,five,six,seven ")
split_by_separator(" one\ttwo\tthree,for,five,six,seven ")
Out[25]:
['one\ttwo\tthree', 'for', 'five', 'six', 'seven']
In [26]:
Copied!
split_by_separator("one|two|three")
split_by_separator("one|two|three")
Out[26]:
['one', 'two', 'three']
In [27]:
Copied!
# only one separator frequency, so the priority will be respect
split_by_separator("one-two three|four", ["-", " ", "|"])
# only one separator frequency, so the priority will be respect
split_by_separator("one-two three|four", ["-", " ", "|"])
Out[27]:
['one', 'two three|four']
In [28]:
Copied!
split_by_separator("one two three four")
split_by_separator("one two three four")
Out[28]:
['one', 'two', 'three', 'four']
⚠️ Type constraint!¶
By default return a error it the type is not a string!
In [29]:
Copied!
try:
split_by_separator(["test with list as a type"])
except Exception as e:
print(e)
try:
split_by_separator(["test with list as a type"])
except Exception as e:
print(e)
Error: The value passed is not a string.
❗ Type constraint Soft!¶
You can use a soft constraint in the type!
In [30]:
Copied!
result = split_by_separator(["test with list as a type"], type_constraint= False)
print(result)
result = split_by_separator(["test with list as a type"], type_constraint= False)
print(result)
Skipping: The value passed is not a string. ['test with list as a type']
Str class - Split by separator¶
In [31]:
Copied!
from strplus import Str
from strplus import Str
In [32]:
Copied!
my_string = Str("one,two,three")
my_string.split_by_separator(",")
my_string = Str("one,two,three")
my_string.split_by_separator(",")
Out[32]:
['one', 'two', 'three']
In [33]:
Copied!
# only one separator frequency, so the priority will be respect
my_string = Str("one-two three|four")
my_string.split_by_separator(["-", " ", "|"])
# only one separator frequency, so the priority will be respect
my_string = Str("one-two three|four")
my_string.split_by_separator(["-", " ", "|"])
Out[33]:
['one', 'two three|four']
In [34]:
Copied!
my_string = Str("one two three four")
my_string.split_by_separator()
my_string = Str("one two three four")
my_string.split_by_separator()
Out[34]:
['one', 'two', 'three', 'four']
In [35]:
Copied!
### Run project local 📀
# import os
# import sys
# sys.path.insert(0, os.path.abspath("../.."))
# sys.path.insert(0, os.path.abspath(".."))
### Run project local 📀
# import os
# import sys
# sys.path.insert(0, os.path.abspath("../.."))
# sys.path.insert(0, os.path.abspath(".."))