Skip to content

Extend your string powers with Str+

Features ✨️

  • Wrapper Class
  • +464 test covered in 36 Tests files!
  • Simple use!
  • Made with A.I. contribution 🤖

Install 📀

pip install strplus

Main Class: 🚀

Str

Bases: str

Str Class

The Str Class is a Wrapper class that extends the str Class, giving it superpowers to handle strings and making it easy to use the strplus functions! You don't need to import, only if you want! All methods from Str are recursive and return the Str object itself, so you always have the same features in the result.

Easy and simple no parentheses!

my_string = Str("Cast_this_StringToSnake")
my_string.snake
cast_this_string_to_snake

my_string = Str("cast_this_string_to_camel")
my_string.camel
castThisStringToCamel

my_string = Str("Cast_this_string_TO_Pascal!")
my_string.pascal
CastThisStringToPascal

Use parentheses if you prefer!

my_string = Str("HelloWorld")
my_string.to_snake()
hello_world

Source code in strplus/strplus.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
class Str(str):
    """

    !!! Info "Str Class"

        The Str Class is a Wrapper class that extends the str Class, giving it superpowers
        to handle strings and making it easy to use the strplus functions! You don't need to import, only if you want!
        All methods from Str are recursive and return the Str object itself, so you always have the same features in the result.

    !!! Example "Easy and simple no parentheses!"

        === "Snake case"
            ```python
            my_string = Str("Cast_this_StringToSnake")
            my_string.snake
            ```
            cast_this_string_to_snake

        === "Camel case"
            ```python
            my_string = Str("cast_this_string_to_camel")
            my_string.camel
            ```
            castThisStringToCamel

        === "Pascal"
            ```python
            my_string = Str("Cast_this_string_TO_Pascal!")
            my_string.pascal
            ```
            CastThisStringToPascal


    !!! Tip "Use parentheses if you prefer!"

        ```python
        my_string = Str("HelloWorld")
        my_string.to_snake()

        ```
        hello_world

    """

    def __new__(cls, *args, **kwargs):
        if not all(isinstance(arg, str) for arg in args):
            raise TypeError("Str argument must be a string")
        return super().__new__(cls, *args, **kwargs)

    def __init__(self, *args, **kwargs):
        super().__init__()

        def __new__(cls, *args, **kwargs):
            return super().__new__(cls, *args, **kwargs)

    @property
    def pascal(self):
        """
        Cast itself to PascalCase!
        This attribute is an alias for [`to_pascal`][strplus.Str.to_pascal]
        """
        return self.to_pascal()

    @property
    def camel(self):
        """
        Cast itself to camelCase!
        This attribute is an alias for [`to_camel`][strplus.Str.to_camel]
        """
        return self.to_camel()

    @property
    def snake(self):
        """
        Cast itself to snake_case!
        This attribute is an alias for [`to_snake`][strplus.Str.to_snake]
        """
        return self.to_snake()

    @property
    def list(self):
        """
        Split itself into a list!
        This attribute is an alias for [`to_list`][strplus.Str.to_list]
        """
        return self.to_list()

    @property
    def split_by_sep(self):
        """
        Splits itself into a list of strings based on the built-in common separators.
        This attribute is an alias for the function[`split_by_separator`][strplus.Str.split_by_separator]

        Tip: Use
            - If you need to to inform a different list in can call the the function split_by_separator from this class
              and inform your custom list of separators!
            - You can find examples of use in menu examples.str_class

        """
        return self.split_by_separator(type_constraint=False)

    @property
    def separator_as_comma(self):
        """
        Looks for a separator in your own value and cast it to comma!
        separator_as_comma is an alias for [`cast_separator_to_comma`][strplus.Str.cast_separator_to_comma]
        """
        return self.cast_separator_to_comma()

    @property
    def print(self):
        """
        Print itself!
        This attribute is an alias for `str.print`
        """
        print(self)

    def to_pascal(self):
        """
        Simple method to converts a string to PascalCase.
        Implementation of [strplus.cases.to_pascal][]

        Returns:
            str: The PascalCase version of the input string.

        !!! Example

            ```python
            my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
            my_string.to_pascal()
            ```
            SomeMixedStringWithSpacesUnderscoresAndHyphens
        """
        return Str(to_pascal(self))

    def to_camel(self):
        """
        Converts a string from any case to CamelCase.
        Implementation of [strplus.cases.to_camel][]

        Returns:
            str: The converted string in CamelCase.

        !!! Example

            === "Example 01"
                ```
                my_string = Str('this_is-an_example')
                my_string.to_camel()
                ```
                thisIsAnExample

            === "Example 02"
                ```
                my_string = Str('This is a test!')
                my_string.to_camel()
                ```
                thisIsATest
        """
        return Str(to_camel(self))

    def to_snake(self):
        """
        Converts a string to snake_case.
        Implementation of [strplus.cases.to_snake][]

        Returns:
            str: The string converted to snake_case.

        !!! Example "Examples"

            === "Example 01"
                ```
                my_string = Str("HelloWorld")
                my_string.snake("HelloWorld")
                ```
                hello_world

            === "Example 02"
                ```
                my_string = Str("  AnotherString!  ")
                my_string.to_snake()
                ```
                another_string

            === "Example 03"
                ```
                my_string = Str("hello-world")
                my_string.to_snake()
                ```
                hello_world
        """
        return Str(to_snake(self))

    def to_list(self):
        """
        Converts a string to a list of strings, where each word is a separate element in the list.
        Implementation of [strplus.functions.to_list][]

        Returns:
            List[str]: A list of strings, where each word in the input string is a separate element in the list.

        !!! Example "Converting a string to a list"
            This example shows how to use `to_list()` to convert a string to a list.

            === "Example 1"
                ```python
                my_string = Str('hello world')
                my_string.to_list
                ```
                ['hello', 'world']


            === "Example 2"
                ```python
                my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
                my_string.to_list
                ```
                ['some', 'mixed', 'string', 'With', 'spaces', 'underscores', 'and', 'hyphens']

            === "Example 2"
                ```python
                my_string = Str('123abc')
                my_string.to_list
                ```
                ['123abc']

        Tip: Use tips
            - If you need to convert a string to a list of integers or floats, you can use list comprehension to convert each element to the desired type.
            - If you need to remove duplicates from the list, you can convert it to a set and then back to a list.

        Info: Important
            - For best results, avoid using punctuation or non-alphanumeric characters in the input string.
            - This function uses regular expressions to identify words in the input string.
        """
        return [Str(word) for word in to_list(self)]

    def split_by_separator(self, separator: Optional[Union[List[str], str]] = None, type_constraint=True):
        """
        Splits a string into a list of strings using the specified separator(s), base in the built-in common separators.
        Implementation of [strplus.functions.split_by_separator][]

        Args:
            input_string (str): The input string to split.
            separator (Optional[Union[List[str], str]], optional): The separator(s) to use when splitting the input string.
                This can be a single string, a list of strings, or None. If None, the function will attempt to determine
                the appropriate separator based on the input string. Defaults to None.

        Returns:
            List[str]: A list of strings resulting from splitting the input string using the specified separator(s).

        !!! Example "This example shows how to use `split_by_separator()` to split a string using a single separator"

            === "Example 1"
                ```python
                my_string = Str("one,two,three")
                my_string.split_by_separator(",")
                ```
                Returns:
                ```
                ["one", "two", "three"]
                ```

            === "Example 2"
                ```python
                my_string = Str("one-two three|four")
                my_string.split_by_separator(["-", " ", "|"])
                ```
                Returns:
                ```
                ['one', 'two three|four']
                ```
                !!! Warning
                    Only one separator frequency found in the list provided, so the priority will be respect!

            === "Example 3"
                ```python
                my_string = Str("one two three four")
                my_string.split_by_separator()
                ```
                Returns:
                ```
                ["one", "two", "three", "four"]
                ```

        Tips:
            - If the input string contains multiple consecutive instances of the specified separator(s), the resulting
            list may contain empty strings. To remove empty strings from the resulting list, you can use a list
            comprehension to filter out any empty strings.
            - See the `get_separator` for mor details about how the function will attempt to determine the appropriate separator.

        Info: Important
            - If the separator is a list of strings, the function will attempt to determine the appropriate separator
            to use based on the input string. If no appropriate separator is found, the function will return the
            original input string as a single-element list.
        """
        result = split_by_separator(self, separator=separator, type_constraint=type_constraint)
        result = [Str(word) for word in result] if isinstance(result, list) else result
        return result

    def cast_separator_to_comma(self, separator: Optional[str] = None):
        """
        Replaces a specified separator or the automatically detected one with a comma in the input string.
        Implementation of [strplus.functions.cast_separator_to_comma][]

        Args:
            input_string (str): The input string to replace separators in.
            separator (Optional[str], optional): The separator to replace with a comma. If None, the function will
                attempt to determine the appropriate separator based on the input string. Defaults to None.

        Returns:
            str: A string resulting from replacing the specified or detected separator with a comma.

        !!! Example "This example shows how to use `cast_separator_to_comma()` to replace a separator in a string"

            === "Example 1"
                ```python
                my_string = Str("one-two-three", "-")
                my_string.cast_separator_to_comma("-")
                ```
                Returns:
                ```
                "one,two,three"
                ```

            === "Example 2"
                ```python
                my_string = Str("one two three")
                my_string.cast_separator_to_comma()
                ```
                Returns:
                ```
                "one,two,three"
                ```
                !!! Warning
                    The function will only attempt to detect the separator when the `separator` argument is None.

        Tips:
            - If the input string does not contain the specified or detected separator, the function will return the
            original input string unchanged.
            - See the `get_separator` function for more details about how the function will attempt to detect the separator.
        """
        return cast_separator_to_comma(self, separator=separator)

    @staticmethod
    def cast(input_value: any, join_sep: str = ",", type_constraint: bool = True):
        """
        Casts the input value to Str+. If it's a list will be joined by comma or you can define you custom separator to join the elements.

        Args:
            input_value (any): The input value to cast.
            join_sep (str, optional): The separator to join list elements with. Defaults to ",".
            type_constraint (bool, optional): If True (default), raise a ValueError if the input value is not a string or a list.

        Returns:
            Str: A string or a string representation of a list of values joined by the specified separator.

        !!! Example "This example shows how to use `cast()` to cast an input value to a string"
            === "Example 1"
                ```python
                Str.cast("Hello, world!")
                ```
                Returns:
                ```
                "Hello, world!"
                ```
                Type: strplus.strplus.Str

            === "Example 2"
                ```python
                Str.cast(["apple", "banana", "orange"], join_sep="-")
                ```
                Returns:
                ```
                "apple-banana-orange"
                ```
                Type: strplus.strplus.Str

            === "Example 3"
                ```python
                Str.cast(123, type_constraint=False)
                ```
                Returns:
                ```
                Skipping: It's not possible to cast from type: <class 'int'>
                123
                ```
                Type: int

        Raises:
            ValueError: If the input value is not a string or a list and `type_constraint` is True.

        Tips:
            - If the input value is a list, the resulting string will be a string representation of the list elements joined by the specified separator.
        """
        if isinstance(input_value, str):
            return Str(input_value)
        elif isinstance(input_value, list):
            return Str(f"{join_sep}".join(input_value))
        else:
            if type_constraint:
                raise ValueError(f"Error: It's not possible to cast from type: {type(input_value)}")
            else:
                print(f"Skipping: It's not possible to cast from type: {type(input_value)}")
                return input_value

camel property

Cast itself to camelCase! This attribute is an alias for to_camel

list property

Split itself into a list! This attribute is an alias for to_list

pascal property

Cast itself to PascalCase! This attribute is an alias for to_pascal

print property

Print itself! This attribute is an alias for str.print

separator_as_comma property

Looks for a separator in your own value and cast it to comma! separator_as_comma is an alias for cast_separator_to_comma

snake property

Cast itself to snake_case! This attribute is an alias for to_snake

split_by_sep property

Splits itself into a list of strings based on the built-in common separators. This attribute is an alias for the functionsplit_by_separator

Use
  • If you need to to inform a different list in can call the the function split_by_separator from this class and inform your custom list of separators!
  • You can find examples of use in menu examples.str_class

cast(input_value, join_sep=',', type_constraint=True) staticmethod

Casts the input value to Str+. If it's a list will be joined by comma or you can define you custom separator to join the elements.

Parameters:

Name Type Description Default
input_value any

The input value to cast.

required
join_sep str

The separator to join list elements with. Defaults to ",".

','
type_constraint bool

If True (default), raise a ValueError if the input value is not a string or a list.

True

Returns:

Name Type Description
Str

A string or a string representation of a list of values joined by the specified separator.

This example shows how to use cast() to cast an input value to a string

Str.cast("Hello, world!")
Returns:
"Hello, world!"
Type: strplus.strplus.Str

Str.cast(["apple", "banana", "orange"], join_sep="-")
Returns:
"apple-banana-orange"
Type: strplus.strplus.Str

Str.cast(123, type_constraint=False)
Returns:
Skipping: It's not possible to cast from type: <class 'int'>
123
Type: int

Raises:

Type Description
ValueError

If the input value is not a string or a list and type_constraint is True.

Tips
  • If the input value is a list, the resulting string will be a string representation of the list elements joined by the specified separator.
Source code in strplus/strplus.py
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
@staticmethod
def cast(input_value: any, join_sep: str = ",", type_constraint: bool = True):
    """
    Casts the input value to Str+. If it's a list will be joined by comma or you can define you custom separator to join the elements.

    Args:
        input_value (any): The input value to cast.
        join_sep (str, optional): The separator to join list elements with. Defaults to ",".
        type_constraint (bool, optional): If True (default), raise a ValueError if the input value is not a string or a list.

    Returns:
        Str: A string or a string representation of a list of values joined by the specified separator.

    !!! Example "This example shows how to use `cast()` to cast an input value to a string"
        === "Example 1"
            ```python
            Str.cast("Hello, world!")
            ```
            Returns:
            ```
            "Hello, world!"
            ```
            Type: strplus.strplus.Str

        === "Example 2"
            ```python
            Str.cast(["apple", "banana", "orange"], join_sep="-")
            ```
            Returns:
            ```
            "apple-banana-orange"
            ```
            Type: strplus.strplus.Str

        === "Example 3"
            ```python
            Str.cast(123, type_constraint=False)
            ```
            Returns:
            ```
            Skipping: It's not possible to cast from type: <class 'int'>
            123
            ```
            Type: int

    Raises:
        ValueError: If the input value is not a string or a list and `type_constraint` is True.

    Tips:
        - If the input value is a list, the resulting string will be a string representation of the list elements joined by the specified separator.
    """
    if isinstance(input_value, str):
        return Str(input_value)
    elif isinstance(input_value, list):
        return Str(f"{join_sep}".join(input_value))
    else:
        if type_constraint:
            raise ValueError(f"Error: It's not possible to cast from type: {type(input_value)}")
        else:
            print(f"Skipping: It's not possible to cast from type: {type(input_value)}")
            return input_value

cast_separator_to_comma(separator=None)

Replaces a specified separator or the automatically detected one with a comma in the input string. Implementation of strplus.functions.cast_separator_to_comma

Parameters:

Name Type Description Default
input_string str

The input string to replace separators in.

required
separator Optional[str]

The separator to replace with a comma. If None, the function will attempt to determine the appropriate separator based on the input string. Defaults to None.

None

Returns:

Name Type Description
str

A string resulting from replacing the specified or detected separator with a comma.

This example shows how to use cast_separator_to_comma() to replace a separator in a string

my_string = Str("one-two-three", "-")
my_string.cast_separator_to_comma("-")
Returns:
"one,two,three"

my_string = Str("one two three")
my_string.cast_separator_to_comma()
Returns:
"one,two,three"

Warning

The function will only attempt to detect the separator when the separator argument is None.

Tips
  • If the input string does not contain the specified or detected separator, the function will return the original input string unchanged.
  • See the get_separator function for more details about how the function will attempt to detect the separator.
Source code in strplus/strplus.py
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
def cast_separator_to_comma(self, separator: Optional[str] = None):
    """
    Replaces a specified separator or the automatically detected one with a comma in the input string.
    Implementation of [strplus.functions.cast_separator_to_comma][]

    Args:
        input_string (str): The input string to replace separators in.
        separator (Optional[str], optional): The separator to replace with a comma. If None, the function will
            attempt to determine the appropriate separator based on the input string. Defaults to None.

    Returns:
        str: A string resulting from replacing the specified or detected separator with a comma.

    !!! Example "This example shows how to use `cast_separator_to_comma()` to replace a separator in a string"

        === "Example 1"
            ```python
            my_string = Str("one-two-three", "-")
            my_string.cast_separator_to_comma("-")
            ```
            Returns:
            ```
            "one,two,three"
            ```

        === "Example 2"
            ```python
            my_string = Str("one two three")
            my_string.cast_separator_to_comma()
            ```
            Returns:
            ```
            "one,two,three"
            ```
            !!! Warning
                The function will only attempt to detect the separator when the `separator` argument is None.

    Tips:
        - If the input string does not contain the specified or detected separator, the function will return the
        original input string unchanged.
        - See the `get_separator` function for more details about how the function will attempt to detect the separator.
    """
    return cast_separator_to_comma(self, separator=separator)

split_by_separator(separator=None, type_constraint=True)

Splits a string into a list of strings using the specified separator(s), base in the built-in common separators. Implementation of strplus.functions.split_by_separator

Parameters:

Name Type Description Default
input_string str

The input string to split.

required
separator Optional[Union[List[str], str]]

The separator(s) to use when splitting the input string. This can be a single string, a list of strings, or None. If None, the function will attempt to determine the appropriate separator based on the input string. Defaults to None.

None

Returns:

Type Description

List[str]: A list of strings resulting from splitting the input string using the specified separator(s).

This example shows how to use split_by_separator() to split a string using a single separator

my_string = Str("one,two,three")
my_string.split_by_separator(",")
Returns:
["one", "two", "three"]

my_string = Str("one-two three|four")
my_string.split_by_separator(["-", " ", "|"])
Returns:
['one', 'two three|four']

Warning

Only one separator frequency found in the list provided, so the priority will be respect!

my_string = Str("one two three four")
my_string.split_by_separator()
Returns:
["one", "two", "three", "four"]

Tips
  • If the input string contains multiple consecutive instances of the specified separator(s), the resulting list may contain empty strings. To remove empty strings from the resulting list, you can use a list comprehension to filter out any empty strings.
  • See the get_separator for mor details about how the function will attempt to determine the appropriate separator.
Important
  • If the separator is a list of strings, the function will attempt to determine the appropriate separator to use based on the input string. If no appropriate separator is found, the function will return the original input string as a single-element list.
Source code in strplus/strplus.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
def split_by_separator(self, separator: Optional[Union[List[str], str]] = None, type_constraint=True):
    """
    Splits a string into a list of strings using the specified separator(s), base in the built-in common separators.
    Implementation of [strplus.functions.split_by_separator][]

    Args:
        input_string (str): The input string to split.
        separator (Optional[Union[List[str], str]], optional): The separator(s) to use when splitting the input string.
            This can be a single string, a list of strings, or None. If None, the function will attempt to determine
            the appropriate separator based on the input string. Defaults to None.

    Returns:
        List[str]: A list of strings resulting from splitting the input string using the specified separator(s).

    !!! Example "This example shows how to use `split_by_separator()` to split a string using a single separator"

        === "Example 1"
            ```python
            my_string = Str("one,two,three")
            my_string.split_by_separator(",")
            ```
            Returns:
            ```
            ["one", "two", "three"]
            ```

        === "Example 2"
            ```python
            my_string = Str("one-two three|four")
            my_string.split_by_separator(["-", " ", "|"])
            ```
            Returns:
            ```
            ['one', 'two three|four']
            ```
            !!! Warning
                Only one separator frequency found in the list provided, so the priority will be respect!

        === "Example 3"
            ```python
            my_string = Str("one two three four")
            my_string.split_by_separator()
            ```
            Returns:
            ```
            ["one", "two", "three", "four"]
            ```

    Tips:
        - If the input string contains multiple consecutive instances of the specified separator(s), the resulting
        list may contain empty strings. To remove empty strings from the resulting list, you can use a list
        comprehension to filter out any empty strings.
        - See the `get_separator` for mor details about how the function will attempt to determine the appropriate separator.

    Info: Important
        - If the separator is a list of strings, the function will attempt to determine the appropriate separator
        to use based on the input string. If no appropriate separator is found, the function will return the
        original input string as a single-element list.
    """
    result = split_by_separator(self, separator=separator, type_constraint=type_constraint)
    result = [Str(word) for word in result] if isinstance(result, list) else result
    return result

to_camel()

Converts a string from any case to CamelCase. Implementation of strplus.cases.to_camel

Returns:

Name Type Description
str

The converted string in CamelCase.

Example

my_string = Str('this_is-an_example')
my_string.to_camel()
thisIsAnExample

my_string = Str('This is a test!')
my_string.to_camel()
thisIsATest

Source code in strplus/strplus.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def to_camel(self):
    """
    Converts a string from any case to CamelCase.
    Implementation of [strplus.cases.to_camel][]

    Returns:
        str: The converted string in CamelCase.

    !!! Example

        === "Example 01"
            ```
            my_string = Str('this_is-an_example')
            my_string.to_camel()
            ```
            thisIsAnExample

        === "Example 02"
            ```
            my_string = Str('This is a test!')
            my_string.to_camel()
            ```
            thisIsATest
    """
    return Str(to_camel(self))

to_list()

Converts a string to a list of strings, where each word is a separate element in the list. Implementation of strplus.functions.to_list

Returns:

Type Description

List[str]: A list of strings, where each word in the input string is a separate element in the list.

Converting a string to a list

This example shows how to use to_list() to convert a string to a list.

my_string = Str('hello world')
my_string.to_list
['hello', 'world']

my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
my_string.to_list
['some', 'mixed', 'string', 'With', 'spaces', 'underscores', 'and', 'hyphens']

my_string = Str('123abc')
my_string.to_list
['123abc']

Use tips
  • If you need to convert a string to a list of integers or floats, you can use list comprehension to convert each element to the desired type.
  • If you need to remove duplicates from the list, you can convert it to a set and then back to a list.
Important
  • For best results, avoid using punctuation or non-alphanumeric characters in the input string.
  • This function uses regular expressions to identify words in the input string.
Source code in strplus/strplus.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
def to_list(self):
    """
    Converts a string to a list of strings, where each word is a separate element in the list.
    Implementation of [strplus.functions.to_list][]

    Returns:
        List[str]: A list of strings, where each word in the input string is a separate element in the list.

    !!! Example "Converting a string to a list"
        This example shows how to use `to_list()` to convert a string to a list.

        === "Example 1"
            ```python
            my_string = Str('hello world')
            my_string.to_list
            ```
            ['hello', 'world']


        === "Example 2"
            ```python
            my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
            my_string.to_list
            ```
            ['some', 'mixed', 'string', 'With', 'spaces', 'underscores', 'and', 'hyphens']

        === "Example 2"
            ```python
            my_string = Str('123abc')
            my_string.to_list
            ```
            ['123abc']

    Tip: Use tips
        - If you need to convert a string to a list of integers or floats, you can use list comprehension to convert each element to the desired type.
        - If you need to remove duplicates from the list, you can convert it to a set and then back to a list.

    Info: Important
        - For best results, avoid using punctuation or non-alphanumeric characters in the input string.
        - This function uses regular expressions to identify words in the input string.
    """
    return [Str(word) for word in to_list(self)]

to_pascal()

Simple method to converts a string to PascalCase. Implementation of strplus.cases.to_pascal

Returns:

Name Type Description
str

The PascalCase version of the input string.

Example

my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
my_string.to_pascal()
SomeMixedStringWithSpacesUnderscoresAndHyphens

Source code in strplus/strplus.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def to_pascal(self):
    """
    Simple method to converts a string to PascalCase.
    Implementation of [strplus.cases.to_pascal][]

    Returns:
        str: The PascalCase version of the input string.

    !!! Example

        ```python
        my_string = Str('some-mixed_string With spaces_underscores-and-hyphens')
        my_string.to_pascal()
        ```
        SomeMixedStringWithSpacesUnderscoresAndHyphens
    """
    return Str(to_pascal(self))

to_snake()

Converts a string to snake_case. Implementation of strplus.cases.to_snake

Returns:

Name Type Description
str

The string converted to snake_case.

Examples

my_string = Str("HelloWorld")
my_string.snake("HelloWorld")
hello_world

my_string = Str("  AnotherString!  ")
my_string.to_snake()
another_string

my_string = Str("hello-world")
my_string.to_snake()
hello_world

Source code in strplus/strplus.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def to_snake(self):
    """
    Converts a string to snake_case.
    Implementation of [strplus.cases.to_snake][]

    Returns:
        str: The string converted to snake_case.

    !!! Example "Examples"

        === "Example 01"
            ```
            my_string = Str("HelloWorld")
            my_string.snake("HelloWorld")
            ```
            hello_world

        === "Example 02"
            ```
            my_string = Str("  AnotherString!  ")
            my_string.to_snake()
            ```
            another_string

        === "Example 03"
            ```
            my_string = Str("hello-world")
            my_string.to_snake()
            ```
            hello_world
    """
    return Str(to_snake(self))