Str class usage! 🏆¶
🚀 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 [3]:
Copied!
from strplus import Str
from strplus import Str
😍 Simples use no parentheses!¶
📜 Print inside!¶
In [4]:
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 [5]:
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
✂️ Str class - Split by separator¶
No parentheses¶
In [6]:
Copied!
from strplus import Str
from strplus import Str
In [7]:
Copied!
my_string = Str("one;two;three;four")
my_string.split_by_sep
my_string = Str("one;two;three;four")
my_string.split_by_sep
Out[7]:
['one', 'two', 'three', 'four']
In [8]:
Copied!
my_string = Str("one,two,three,four")
my_string.split_by_sep
my_string = Str("one,two,three,four")
my_string.split_by_sep
Out[8]:
['one', 'two', 'three', 'four']
In [9]:
Copied!
my_string = Str("one|two|three|four")
my_string.split_by_sep
my_string = Str("one|two|three|four")
my_string.split_by_sep
Out[9]:
['one', 'two', 'three', 'four']
With parentheses¶
In [10]:
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[10]:
['one', 'two', 'three', 'four']
In [11]:
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[11]:
['one', 'two', 'three', 'four']
In [12]:
Copied!
my_string = Str("one,two,three")
my_string.split_by_separator(",")
my_string = Str("one,two,three")
my_string.split_by_separator(",")
Out[12]:
['one', 'two', 'three']
⚠️ Only one separator frequency, so the priority will be respect
In [13]:
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[13]:
['one', 'two three|four']
📎 String Cases¶
In [14]:
Copied!
from strplus import Str
from strplus import Str
In [15]:
Copied!
my_string = Str("Cast_this_string_TO_Pascal!")
my_string.pascal
my_string = Str("Cast_this_string_TO_Pascal!")
my_string.pascal
Out[15]:
'CastThisStringToPascal'
In [16]:
Copied!
my_string = Str("cast_this_string_to_camel")
my_string.camel
my_string = Str("cast_this_string_to_camel")
my_string.camel
Out[16]:
'castThisStringToCamel'
In [17]:
Copied!
my_string = Str("Cast_this_StringToSnake")
my_string.snake
my_string = Str("Cast_this_StringToSnake")
my_string.snake
Out[17]:
'cast_this_string_to_snake'
📐 Cast separator to comma.¶
[most frequent separator or the given one!]
In [18]:
Copied!
from strplus import Str
from strplus import Str
In [19]:
Copied!
my_string = Str("test;one;")
my_string.separator_as_comma
my_string = Str("test;one;")
my_string.separator_as_comma
Out[19]:
'test,one,'
In [20]:
Copied!
my_string = Str("|test|one|")
my_string.separator_as_comma
my_string = Str("|test|one|")
my_string.separator_as_comma
Out[20]:
',test,one,'
In [21]:
Copied!
my_string = Str("test:one")
my_string.separator_as_comma
my_string = Str("test:one")
my_string.separator_as_comma
Out[21]:
'test,one'
🌱 Cast¶
In [22]:
Copied!
from strplus import Str
from strplus import Str
In [23]:
Copied!
my_string = Str.cast("Hello, world!")
print(f"my_string, \ntype: {type(my_string)}, \nvalue: {my_string} \ninstance of str: {isinstance(my_string, str)}")
my_string = Str.cast("Hello, world!")
print(f"my_string, \ntype: {type(my_string)}, \nvalue: {my_string} \ninstance of str: {isinstance(my_string, str)}")
my_string, type: <class 'strplus.strplus.Str'>, value: Hello, world! instance of str: True
In [24]:
Copied!
my_string = Str.cast(["apple", "banana", "orange"], join_sep="-")
print(f"my_string, \ntype: {type(my_string)}, \nvalue: {my_string} \ninstance of str: {isinstance(my_string, str)}")
my_string = Str.cast(["apple", "banana", "orange"], join_sep="-")
print(f"my_string, \ntype: {type(my_string)}, \nvalue: {my_string} \ninstance of str: {isinstance(my_string, str)}")
my_string, type: <class 'strplus.strplus.Str'>, value: apple-banana-orange instance of str: True
In [25]:
Copied!
my_string = Str.cast(123, type_constraint=False)
print(f"my_string, \ntype: {type(my_string)}, \nvalue: {my_string} \ninstance of str: {isinstance(my_string, str)}")
my_string = Str.cast(123, type_constraint=False)
print(f"my_string, \ntype: {type(my_string)}, \nvalue: {my_string} \ninstance of str: {isinstance(my_string, str)}")
Skipping: It's not possible to cast from type: <class 'int'> my_string, type: <class 'int'>, value: 123 instance of str: False
In [26]:
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(".."))