0
# Strings Module
1
2
The Strings module provides 69 functions for comprehensive string manipulation and formatting. These functions cover case conversion, padding, trimming, searching, transformation, and various string utilities.
3
4
## Case Conversion Functions
5
6
### camel_case
7
8
```python { .api }
9
def camel_case(text: Any) -> str
10
```
11
12
Converts `text` to camelCase.
13
14
**Parameters:**
15
- `text` (`Any`): String to convert.
16
17
**Returns:**
18
- `str`: String in camelCase.
19
20
**Example:**
21
```python { .api }
22
from pydash import camel_case
23
24
camel_case('Foo Bar')
25
# 'fooBar'
26
27
camel_case('--foo-bar--')
28
# 'fooBar'
29
30
camel_case('__FOO_BAR__')
31
# 'fooBar'
32
```
33
34
### kebab_case
35
36
```python { .api }
37
def kebab_case(text: Any) -> str
38
```
39
40
Converts `text` to kebab-case.
41
42
**Parameters:**
43
- `text` (`Any`): String to convert.
44
45
**Returns:**
46
- `str`: String in kebab-case.
47
48
**Example:**
49
```python { .api }
50
from pydash import kebab_case
51
52
kebab_case('Foo Bar')
53
# 'foo-bar'
54
55
kebab_case('fooBar')
56
# 'foo-bar'
57
58
kebab_case('__FOO_BAR__')
59
# 'foo-bar'
60
```
61
62
### snake_case
63
64
```python { .api }
65
def snake_case(text: Any) -> str
66
```
67
68
Converts `text` to snake_case.
69
70
**Parameters:**
71
- `text` (`Any`): String to convert.
72
73
**Returns:**
74
- `str`: String in snake_case.
75
76
**Example:**
77
```python { .api }
78
from pydash import snake_case
79
80
snake_case('Foo Bar')
81
# 'foo_bar'
82
83
snake_case('fooBar')
84
# 'foo_bar'
85
86
snake_case('--FOO-BAR--')
87
# 'foo_bar'
88
```
89
90
### pascal_case
91
92
```python { .api }
93
def pascal_case(text: Any, strict: bool = True) -> str
94
```
95
96
Converts `text` to PascalCase.
97
98
**Parameters:**
99
- `text` (`Any`): String to convert.
100
- `strict` (`bool`): Whether to force strict camel case. Defaults to `True`.
101
102
**Returns:**
103
- `str`: String in PascalCase.
104
105
**Example:**
106
```python { .api }
107
from pydash import pascal_case
108
109
pascal_case('foo bar')
110
# 'FooBar'
111
112
pascal_case('--foo-bar--')
113
# 'FooBar'
114
115
pascal_case('fooBar')
116
# 'FooBar'
117
```
118
119
### start_case
120
121
```python { .api }
122
def start_case(text: Any) -> str
123
```
124
125
Converts `text` to Start Case.
126
127
**Parameters:**
128
- `text` (`Any`): String to convert.
129
130
**Returns:**
131
- `str`: String in Start Case.
132
133
**Example:**
134
```python { .api }
135
from pydash import start_case
136
137
start_case('--foo-bar--')
138
# 'Foo Bar'
139
140
start_case('fooBar')
141
# 'Foo Bar'
142
143
start_case('__FOO_BAR__')
144
# 'FOO BAR'
145
```
146
147
### lower_case
148
149
```python { .api }
150
def lower_case(text: Any) -> str
151
```
152
153
Converts `text`, as space separated words, to lower case.
154
155
**Parameters:**
156
- `text` (`Any`): String to convert.
157
158
**Returns:**
159
- `str`: String in lower case with spaces.
160
161
**Example:**
162
```python { .api }
163
from pydash import lower_case
164
165
lower_case('--Foo-Bar--')
166
# 'foo bar'
167
168
lower_case('fooBar')
169
# 'foo bar'
170
171
lower_case('__FOO_BAR__')
172
# 'foo bar'
173
```
174
175
### upper_case
176
177
```python { .api }
178
def upper_case(text: Any) -> str
179
```
180
181
Converts `text`, as space separated words, to upper case.
182
183
**Parameters:**
184
- `text` (`Any`): String to convert.
185
186
**Returns:**
187
- `str`: String in upper case with spaces.
188
189
**Example:**
190
```python { .api }
191
from pydash import upper_case
192
193
upper_case('--foo-bar--')
194
# 'FOO BAR'
195
196
upper_case('fooBar')
197
# 'FOO BAR'
198
199
upper_case('__foo_bar__')
200
# 'FOO BAR'
201
```
202
203
### title_case
204
205
```python { .api }
206
def title_case(text: Any) -> str
207
```
208
209
Converts `text` to Title Case.
210
211
**Parameters:**
212
- `text` (`Any`): String to convert.
213
214
**Returns:**
215
- `str`: String in Title Case.
216
217
**Example:**
218
```python { .api }
219
from pydash import title_case
220
221
title_case('--foo-bar--')
222
# 'Foo Bar'
223
224
title_case('fooBar')
225
# 'Foo Bar'
226
```
227
228
### human_case
229
230
```python { .api }
231
def human_case(text: Any) -> str
232
```
233
234
Converts `text` to human readable form by replacing dashes, underscores, and camel case with spaces.
235
236
**Parameters:**
237
- `text` (`Any`): String to convert.
238
239
**Returns:**
240
- `str`: String in human readable form.
241
242
**Example:**
243
```python { .api }
244
from pydash import human_case
245
246
human_case('--foo-bar--')
247
# 'Foo bar'
248
249
human_case('fooBar')
250
# 'Foo bar'
251
```
252
253
### capitalize
254
255
```python { .api }
256
def capitalize(text: Any, strict: bool = True) -> str
257
```
258
259
Converts the first character of `text` to upper case and the remaining to lower case.
260
261
**Parameters:**
262
- `text` (`Any`): String to capitalize.
263
- `strict` (`bool`): Whether to cast rest of string to lower case. Defaults to `True`.
264
265
**Returns:**
266
- `str`: Capitalized string.
267
268
**Example:**
269
```python { .api }
270
from pydash import capitalize
271
272
capitalize('FRED')
273
# 'Fred'
274
275
capitalize('fRED', False)
276
# 'FRED'
277
```
278
279
### decapitalize
280
281
```python { .api }
282
def decapitalize(text: Any) -> str
283
```
284
285
Converts the first character of `text` to lower case.
286
287
**Parameters:**
288
- `text` (`Any`): String to decapitalize.
289
290
**Returns:**
291
- `str`: Decapitalized string.
292
293
**Example:**
294
```python { .api }
295
from pydash import decapitalize
296
297
decapitalize('Fred')
298
# 'fred'
299
300
decapitalize('FRED')
301
# 'fRED'
302
```
303
304
### lower_first
305
306
```python { .api }
307
def lower_first(text: str) -> str
308
```
309
310
Converts the first character of `text` to lower case.
311
312
**Parameters:**
313
- `text` (`str`): String to convert.
314
315
**Returns:**
316
- `str`: Converted string.
317
318
**Example:**
319
```python { .api }
320
from pydash import lower_first
321
322
lower_first('Fred')
323
# 'fred'
324
325
lower_first('FRED')
326
# 'fRED'
327
```
328
329
### upper_first
330
331
```python { .api }
332
def upper_first(text: str) -> str
333
```
334
335
Converts the first character of `text` to upper case.
336
337
**Parameters:**
338
- `text` (`str`): String to convert.
339
340
**Returns:**
341
- `str`: Converted string.
342
343
**Example:**
344
```python { .api }
345
from pydash import upper_first
346
347
upper_first('fred')
348
# 'Fred'
349
350
upper_first('FRED')
351
# 'FRED'
352
```
353
354
### swap_case
355
356
```python { .api }
357
def swap_case(text: Any) -> str
358
```
359
360
Swaps the case of each character in `text`.
361
362
**Parameters:**
363
- `text` (`Any`): String to swap case.
364
365
**Returns:**
366
- `str`: String with swapped case.
367
368
**Example:**
369
```python { .api }
370
from pydash import swap_case
371
372
swap_case('Hello World')
373
# 'hELLO wORLD'
374
```
375
376
### to_lower
377
378
```python { .api }
379
def to_lower(text: Any) -> str
380
```
381
382
Converts `text` to lower case.
383
384
**Parameters:**
385
- `text` (`Any`): String to convert.
386
387
**Returns:**
388
- `str`: Lower case string.
389
390
**Example:**
391
```python { .api }
392
from pydash import to_lower
393
394
to_lower('--Foo-Bar--')
395
# '--foo-bar--'
396
397
to_lower('fooBar')
398
# 'foobar'
399
```
400
401
### to_upper
402
403
```python { .api }
404
def to_upper(text: Any) -> str
405
```
406
407
Converts `text` to upper case.
408
409
**Parameters:**
410
- `text` (`Any`): String to convert.
411
412
**Returns:**
413
- `str`: Upper case string.
414
415
**Example:**
416
```python { .api }
417
from pydash import to_upper
418
419
to_upper('--foo-bar--')
420
# '--FOO-BAR--'
421
422
to_upper('fooBar')
423
# 'FOOBAR'
424
```
425
426
## String Padding and Alignment
427
428
### pad
429
430
```python { .api }
431
def pad(text: Any, length: int, chars: Any = ' ') -> str
432
```
433
434
Pads `text` on the left and right sides if it's shorter than `length`. Padding characters are truncated if they can't be evenly divided by `length`.
435
436
**Parameters:**
437
- `text` (`Any`): String to pad.
438
- `length` (`int`): Padding length.
439
- `chars` (`Any`): String used as padding. Defaults to `' '`.
440
441
**Returns:**
442
- `str`: Padded string.
443
444
**Example:**
445
```python { .api }
446
from pydash import pad
447
448
pad('abc', 8)
449
# ' abc '
450
451
pad('abc', 8, '_-')
452
# '_-abc_-_'
453
454
pad('abc', 3)
455
# 'abc'
456
```
457
458
### pad_end
459
460
```python { .api }
461
def pad_end(text: Any, length: int, chars: Any = ' ') -> str
462
```
463
464
Pads `text` on the right side if it's shorter than `length`.
465
466
**Parameters:**
467
- `text` (`Any`): String to pad.
468
- `length` (`int`): Padding length.
469
- `chars` (`Any`): String used as padding. Defaults to `' '`.
470
471
**Returns:**
472
- `str`: Right padded string.
473
474
**Example:**
475
```python { .api }
476
from pydash import pad_end
477
478
pad_end('abc', 6)
479
# 'abc '
480
481
pad_end('abc', 6, '_-')
482
# 'abc_-_'
483
```
484
485
### pad_start
486
487
```python { .api }
488
def pad_start(text: Any, length: int, chars: Any = ' ') -> str
489
```
490
491
Pads `text` on the left side if it's shorter than `length`.
492
493
**Parameters:**
494
- `text` (`Any`): String to pad.
495
- `length` (`int`): Padding length.
496
- `chars` (`Any`): String used as padding. Defaults to `' '`.
497
498
**Returns:**
499
- `str`: Left padded string.
500
501
**Example:**
502
```python { .api }
503
from pydash import pad_start
504
505
pad_start('abc', 6)
506
# ' abc'
507
508
pad_start('abc', 6, '_-')
509
# '_-_abc'
510
```
511
512
## String Trimming Functions
513
514
### trim
515
516
```python { .api }
517
def trim(text: Any, chars: Any = None) -> str
518
```
519
520
Removes leading and trailing whitespace or specified characters from `text`.
521
522
**Parameters:**
523
- `text` (`Any`): String to trim.
524
- `chars` (`Any`, optional): Characters to trim.
525
526
**Returns:**
527
- `str`: Trimmed string.
528
529
**Example:**
530
```python { .api }
531
from pydash import trim
532
533
trim(' abc ')
534
# 'abc'
535
536
trim('-_-abc-_-', '_-')
537
# 'abc'
538
```
539
540
### trim_end
541
542
```python { .api }
543
def trim_end(text: Any, chars: Any = None) -> str
544
```
545
546
Removes trailing whitespace or specified characters from `text`.
547
548
**Parameters:**
549
- `text` (`Any`): String to trim.
550
- `chars` (`Any`, optional): Characters to trim.
551
552
**Returns:**
553
- `str`: Trimmed string.
554
555
**Example:**
556
```python { .api }
557
from pydash import trim_end
558
559
trim_end(' abc ')
560
# ' abc'
561
562
trim_end('-_-abc-_-', '_-')
563
# '-_-abc'
564
```
565
566
### trim_start
567
568
```python { .api }
569
def trim_start(text: Any, chars: Any = None) -> str
570
```
571
572
Removes leading whitespace or specified characters from `text`.
573
574
**Parameters:**
575
- `text` (`Any`): String to trim.
576
- `chars` (`Any`, optional): Characters to trim.
577
578
**Returns:**
579
- `str`: Trimmed string.
580
581
**Example:**
582
```python { .api }
583
from pydash import trim_start
584
585
trim_start(' abc ')
586
# 'abc '
587
588
trim_start('-_-abc-_-', '_-')
589
# 'abc-_-'
590
```
591
592
## String Search and Test Functions
593
594
### starts_with
595
596
```python { .api }
597
def starts_with(text: Any, target: Any, position: int = 0) -> bool
598
```
599
600
Checks if `text` starts with the given target string.
601
602
**Parameters:**
603
- `text` (`Any`): String to inspect.
604
- `target` (`Any`): String to search for.
605
- `position` (`int`): Position to search from. Defaults to `0`.
606
607
**Returns:**
608
- `bool`: Whether `text` starts with `target`.
609
610
**Example:**
611
```python { .api }
612
from pydash import starts_with
613
614
starts_with('abc', 'a')
615
# True
616
617
starts_with('abc', 'b')
618
# False
619
620
starts_with('abc', 'b', 1)
621
# True
622
```
623
624
### ends_with
625
626
```python { .api }
627
def ends_with(text: Any, target: Any, position: Union[int, None] = None) -> bool
628
```
629
630
Checks if `text` ends with the given target string.
631
632
**Parameters:**
633
- `text` (`Any`): String to inspect.
634
- `target` (`Any`): String to search for.
635
- `position` (`Union[int, None]`): Position to search up to.
636
637
**Returns:**
638
- `bool`: Whether `text` ends with `target`.
639
640
**Example:**
641
```python { .api }
642
from pydash import ends_with
643
644
ends_with('abc', 'c')
645
# True
646
647
ends_with('abc', 'b')
648
# False
649
650
ends_with('abc', 'b', 2)
651
# True
652
```
653
654
### has_substr
655
656
```python { .api }
657
def has_substr(text: Any, subtext: Any) -> bool
658
```
659
660
Checks if `text` contains `subtext`.
661
662
**Parameters:**
663
- `text` (`Any`): String to inspect.
664
- `subtext` (`Any`): String to search for.
665
666
**Returns:**
667
- `bool`: Whether `text` contains `subtext`.
668
669
**Example:**
670
```python { .api }
671
from pydash import has_substr
672
673
has_substr('hello world', 'world')
674
# True
675
676
has_substr('hello world', 'foo')
677
# False
678
```
679
680
### count_substr
681
682
```python { .api }
683
def count_substr(text: Any, subtext: Any) -> int
684
```
685
686
Counts the number of non-overlapping occurrences of `subtext` in `text`.
687
688
**Parameters:**
689
- `text` (`Any`): String to inspect.
690
- `subtext` (`Any`): String to search for.
691
692
**Returns:**
693
- `int`: Number of occurrences.
694
695
**Example:**
696
```python { .api }
697
from pydash import count_substr
698
699
count_substr('hello world world', 'world')
700
# 2
701
702
count_substr('aaa', 'aa')
703
# 1
704
```
705
706
## String Manipulation Functions
707
708
### chop
709
710
```python { .api }
711
def chop(text: Any, step: int) -> List[str]
712
```
713
714
Break up `text` into a list of strings of length `step`.
715
716
**Parameters:**
717
- `text` (`Any`): String to chop.
718
- `step` (`int`): Length of each chunk.
719
720
**Returns:**
721
- `List[str]`: List of chopped strings.
722
723
**Example:**
724
```python { .api }
725
from pydash import chop
726
727
chop('abcdefg', 3)
728
# ['abc', 'def', 'g']
729
```
730
731
### chop_right
732
733
```python { .api }
734
def chop_right(text: Any, step: int) -> List[str]
735
```
736
737
Like `chop` except that it starts from the right.
738
739
**Parameters:**
740
- `text` (`Any`): String to chop.
741
- `step` (`int`): Length of each chunk.
742
743
**Returns:**
744
- `List[str]`: List of chopped strings.
745
746
**Example:**
747
```python { .api }
748
from pydash import chop_right
749
750
chop_right('abcdefg', 3)
751
# ['a', 'bcd', 'efg']
752
```
753
754
### chars
755
756
```python { .api }
757
def chars(text: Any) -> List[str]
758
```
759
760
Split `text` into a list of single characters.
761
762
**Parameters:**
763
- `text` (`Any`): String to split into characters.
764
765
**Returns:**
766
- `List[str]`: List of single characters.
767
768
**Example:**
769
```python { .api }
770
from pydash import chars
771
772
chars('abc')
773
# ['a', 'b', 'c']
774
```
775
776
### clean
777
778
```python { .api }
779
def clean(text: Any) -> str
780
```
781
782
Trim `text` and replace multiple consecutive whitespace with a single space.
783
784
**Parameters:**
785
- `text` (`Any`): String to clean.
786
787
**Returns:**
788
- `str`: Cleaned string.
789
790
**Example:**
791
```python { .api }
792
from pydash import clean
793
794
clean(' hello world ')
795
# 'hello world'
796
```
797
798
### lines
799
800
```python { .api }
801
def lines(text: Any) -> List[str]
802
```
803
804
Split `text` by line breaks and return list of lines.
805
806
**Parameters:**
807
- `text` (`Any`): String to split.
808
809
**Returns:**
810
- `List[str]`: List of lines.
811
812
**Example:**
813
```python { .api }
814
from pydash import lines
815
816
lines('a\nb\r\nc')
817
# ['a', 'b', 'c']
818
```
819
820
### words
821
822
```python { .api }
823
def words(text: Any, pattern: Any = None) -> List[str]
824
```
825
826
Split `text` into a list of words.
827
828
**Parameters:**
829
- `text` (`Any`): String to split into words.
830
- `pattern` (`Any`, optional): Pattern to use for splitting.
831
832
**Returns:**
833
- `List[str]`: List of words.
834
835
**Example:**
836
```python { .api }
837
from pydash import words
838
839
words('fred, barney, & pebbles')
840
# ['fred', 'barney', 'pebbles']
841
842
words('fred, barney, & pebbles', r'[^, ]+')
843
# ['fred', 'barney', '&', 'pebbles']
844
```
845
846
### split
847
848
```python { .api }
849
def split(text: Any, separator: Any = None, limit: Union[int, None] = None) -> List[str]
850
```
851
852
Split `text` by `separator`. If `limit` is specified, the resulting list will have at most `limit` elements.
853
854
**Parameters:**
855
- `text` (`Any`): String to split.
856
- `separator` (`Any`, optional): Separator to split by.
857
- `limit` (`Union[int, None]`): Maximum number of splits.
858
859
**Returns:**
860
- `List[str]`: List of split strings.
861
862
**Example:**
863
```python { .api }
864
from pydash import split
865
866
split('a-b-c', '-')
867
# ['a', 'b', 'c']
868
869
split('a-b-c', '-', 2)
870
# ['a', 'b-c']
871
```
872
873
## String Prefix/Suffix Operations
874
875
### ensure_starts_with
876
877
```python { .api }
878
def ensure_starts_with(text: Any, prefix: Any) -> str
879
```
880
881
If `text` does not start with `prefix`, append `prefix` to the beginning of `text`.
882
883
**Parameters:**
884
- `text` (`Any`): String to check.
885
- `prefix` (`Any`): Prefix to ensure.
886
887
**Returns:**
888
- `str`: String that starts with `prefix`.
889
890
**Example:**
891
```python { .api }
892
from pydash import ensure_starts_with
893
894
ensure_starts_with('foobar', 'foo')
895
# 'foobar'
896
897
ensure_starts_with('bar', 'foo')
898
# 'foobar'
899
```
900
901
### ensure_ends_with
902
903
```python { .api }
904
def ensure_ends_with(text: Any, suffix: Any) -> str
905
```
906
907
If `text` does not end with `suffix`, append `suffix` to `text`.
908
909
**Parameters:**
910
- `text` (`Any`): String to check.
911
- `suffix` (`Any`): Suffix to ensure.
912
913
**Returns:**
914
- `str`: String that ends with `suffix`.
915
916
**Example:**
917
```python { .api }
918
from pydash import ensure_ends_with
919
920
ensure_ends_with('foobar', 'bar')
921
# 'foobar'
922
923
ensure_ends_with('foo', 'bar')
924
# 'foobar'
925
```
926
927
### replace_start
928
929
```python { .api }
930
def replace_start(text: Any, target: Any, replacement: Any) -> str
931
```
932
933
Replace `target` with `replacement` if `text` starts with `target`.
934
935
**Parameters:**
936
- `text` (`Any`): String to modify.
937
- `target` (`Any`): String to replace.
938
- `replacement` (`Any`): Replacement string.
939
940
**Returns:**
941
- `str`: Modified string.
942
943
**Example:**
944
```python { .api }
945
from pydash import replace_start
946
947
replace_start('foobar', 'foo', 'baz')
948
# 'bazbar'
949
950
replace_start('foobar', 'qux', 'baz')
951
# 'foobar'
952
```
953
954
### replace_end
955
956
```python { .api }
957
def replace_end(text: Any, target: Any, replacement: Any) -> str
958
```
959
960
Replace `target` with `replacement` if `text` ends with `target`.
961
962
**Parameters:**
963
- `text` (`Any`): String to modify.
964
- `target` (`Any`): String to replace.
965
- `replacement` (`Any`): Replacement string.
966
967
**Returns:**
968
- `str`: Modified string.
969
970
**Example:**
971
```python { .api }
972
from pydash import replace_end
973
974
replace_end('foobar', 'bar', 'baz')
975
# 'foobaz'
976
977
replace_end('foobar', 'qux', 'baz')
978
# 'foobar'
979
```
980
981
## String Extraction Functions
982
983
### substr_left
984
985
```python { .api }
986
def substr_left(text: Any, subtext: Any) -> str
987
```
988
989
Return substring of `text` before the first occurrence of `subtext`.
990
991
**Parameters:**
992
- `text` (`Any`): String to extract from.
993
- `subtext` (`Any`): String to search for.
994
995
**Returns:**
996
- `str`: Substring before first occurrence of `subtext`.
997
998
**Example:**
999
```python { .api }
1000
from pydash import substr_left
1001
1002
substr_left('foobar', 'b')
1003
# 'foo'
1004
1005
substr_left('foobar', 'z')
1006
# 'foobar'
1007
```
1008
1009
### substr_left_end
1010
1011
```python { .api }
1012
def substr_left_end(text: Any, subtext: Any) -> str
1013
```
1014
1015
Return substring of `text` before the last occurrence of `subtext`.
1016
1017
**Parameters:**
1018
- `text` (`Any`): String to extract from.
1019
- `subtext` (`Any`): String to search for.
1020
1021
**Returns:**
1022
- `str`: Substring before last occurrence of `subtext`.
1023
1024
**Example:**
1025
```python { .api }
1026
from pydash import substr_left_end
1027
1028
substr_left_end('foobarbar', 'bar')
1029
# 'foobar'
1030
```
1031
1032
### substr_right
1033
1034
```python { .api }
1035
def substr_right(text: Any, subtext: Any) -> str
1036
```
1037
1038
Return substring of `text` after the first occurrence of `subtext`.
1039
1040
**Parameters:**
1041
- `text` (`Any`): String to extract from.
1042
- `subtext` (`Any`): String to search for.
1043
1044
**Returns:**
1045
- `str`: Substring after first occurrence of `subtext`.
1046
1047
**Example:**
1048
```python { .api }
1049
from pydash import substr_right
1050
1051
substr_right('foobar', 'o')
1052
# 'obar'
1053
1054
substr_right('foobar', 'z')
1055
# ''
1056
```
1057
1058
### substr_right_end
1059
1060
```python { .api }
1061
def substr_right_end(text: Any, subtext: Any) -> str
1062
```
1063
1064
Return substring of `text` after the last occurrence of `subtext`.
1065
1066
**Parameters:**
1067
- `text` (`Any`): String to extract from.
1068
- `subtext` (`Any`): String to search for.
1069
1070
**Returns:**
1071
- `str`: Substring after last occurrence of `subtext`.
1072
1073
**Example:**
1074
```python { .api }
1075
from pydash import substr_right_end
1076
1077
substr_right_end('foobarbar', 'bar')
1078
# 'bar'
1079
```
1080
1081
## String Transformation Functions
1082
1083
### deburr
1084
1085
```python { .api }
1086
def deburr(text: Any) -> str
1087
```
1088
1089
Converts Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removes combining diacritical marks.
1090
1091
**Parameters:**
1092
- `text` (`Any`): String to deburr.
1093
1094
**Returns:**
1095
- `str`: Deburred string.
1096
1097
**Example:**
1098
```python { .api }
1099
from pydash import deburr
1100
1101
deburr('déjà vu')
1102
# 'deja vu'
1103
```
1104
1105
### escape
1106
1107
```python { .api }
1108
def escape(text: Any) -> str
1109
```
1110
1111
Converts the characters "&", "<", ">", '"', and "'" in `text` to their corresponding HTML entities.
1112
1113
**Parameters:**
1114
- `text` (`Any`): String to escape.
1115
1116
**Returns:**
1117
- `str`: HTML escaped string.
1118
1119
**Example:**
1120
```python { .api }
1121
from pydash import escape
1122
1123
escape('fred, barney, & pebbles')
1124
# 'fred, barney, & pebbles'
1125
```
1126
1127
### unescape
1128
1129
```python { .api }
1130
def unescape(text: Any) -> str
1131
```
1132
1133
The inverse of `escape`. Converts the HTML entities "&", "<", ">", """, and "'" in `text` to their corresponding characters.
1134
1135
**Parameters:**
1136
- `text` (`Any`): String to unescape.
1137
1138
**Returns:**
1139
- `str`: HTML unescaped string.
1140
1141
**Example:**
1142
```python { .api }
1143
from pydash import unescape
1144
1145
unescape('fred, barney, & pebbles')
1146
# 'fred, barney, & pebbles'
1147
```
1148
1149
### escape_reg_exp
1150
1151
```python { .api }
1152
def escape_reg_exp(text: Any) -> str
1153
```
1154
1155
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in `text`.
1156
1157
**Parameters:**
1158
- `text` (`Any`): String to escape.
1159
1160
**Returns:**
1161
- `str`: Escaped string.
1162
1163
**Example:**
1164
```python { .api }
1165
from pydash import escape_reg_exp
1166
1167
escape_reg_exp('[pydash](https://python.org/)')
1168
# '\\[pydash\\]\\(https://python\\.org/\\)'
1169
```
1170
1171
### quote
1172
1173
```python { .api }
1174
def quote(text: Any, quote_char: Any = '"') -> str
1175
```
1176
1177
Quote `text` with `quote_char`.
1178
1179
**Parameters:**
1180
- `text` (`Any`): String to quote.
1181
- `quote_char` (`Any`): Character to quote with. Defaults to `'"'`.
1182
1183
**Returns:**
1184
- `str`: Quoted string.
1185
1186
**Example:**
1187
```python { .api }
1188
from pydash import quote
1189
1190
quote('hello')
1191
# '"hello"'
1192
1193
quote('hello', "'")
1194
# "'hello'"
1195
```
1196
1197
### unquote
1198
1199
```python { .api }
1200
def unquote(text: Any, quote_char: Any = '"') -> str
1201
```
1202
1203
Unquote `text` by removing `quote_char` from beginning and end.
1204
1205
**Parameters:**
1206
- `text` (`Any`): String to unquote.
1207
- `quote_char` (`Any`): Quote character to remove. Defaults to `'"'`.
1208
1209
**Returns:**
1210
- `str`: Unquoted string.
1211
1212
**Example:**
1213
```python { .api }
1214
from pydash import unquote
1215
1216
unquote('"hello"')
1217
# 'hello'
1218
1219
unquote("'hello'", "'")
1220
# 'hello'
1221
```
1222
1223
### surround
1224
1225
```python { .api }
1226
def surround(text: Any, wrapper: Any) -> str
1227
```
1228
1229
Surround `text` with `wrapper`.
1230
1231
**Parameters:**
1232
- `text` (`Any`): String to surround.
1233
- `wrapper` (`Any`): String to wrap with.
1234
1235
**Returns:**
1236
- `str`: Surrounded string.
1237
1238
**Example:**
1239
```python { .api }
1240
from pydash import surround
1241
1242
surround('abc', '"')
1243
# '"abc"'
1244
1245
surround('abc', '!')
1246
# '!abc!'
1247
```
1248
1249
## String Repetition and Generation
1250
1251
### repeat
1252
1253
```python { .api }
1254
def repeat(text: Any, n: int = 0) -> str
1255
```
1256
1257
Repeats the given string `n` times.
1258
1259
**Parameters:**
1260
- `text` (`Any`): String to repeat.
1261
- `n` (`int`): Number of times to repeat. Defaults to `0`.
1262
1263
**Returns:**
1264
- `str`: Repeated string.
1265
1266
**Example:**
1267
```python { .api }
1268
from pydash import repeat
1269
1270
repeat('*', 3)
1271
# '***'
1272
1273
repeat('abc', 2)
1274
# 'abcabc'
1275
1276
repeat('abc', 0)
1277
# ''
1278
```
1279
1280
### predecessor
1281
1282
```python { .api }
1283
def predecessor(char: Any) -> str
1284
```
1285
1286
Return the predecessor character of `char`.
1287
1288
**Parameters:**
1289
- `char` (`Any`): Character to get predecessor of.
1290
1291
**Returns:**
1292
- `str`: Predecessor character.
1293
1294
**Example:**
1295
```python { .api }
1296
from pydash import predecessor
1297
1298
predecessor('c')
1299
# 'b'
1300
1301
predecessor('C')
1302
# 'B'
1303
1304
predecessor('3')
1305
# '2'
1306
```
1307
1308
### successor
1309
1310
```python { .api }
1311
def successor(char: Any) -> str
1312
```
1313
1314
Return the successor character of `char`.
1315
1316
**Parameters:**
1317
- `char` (`Any`): Character to get successor of.
1318
1319
**Returns:**
1320
- `str`: Successor character.
1321
1322
**Example:**
1323
```python { .api }
1324
from pydash import successor
1325
1326
successor('a')
1327
# 'b'
1328
1329
successor('A')
1330
# 'B'
1331
1332
successor('1')
1333
# '2'
1334
```
1335
1336
## String Formatting Functions
1337
1338
### insert_substr
1339
1340
```python { .api }
1341
def insert_substr(text: Any, index: int, subtext: Any) -> str
1342
```
1343
1344
Insert `subtext` into `text` starting at `index`.
1345
1346
**Parameters:**
1347
- `text` (`Any`): String to modify.
1348
- `index` (`int`): Index to insert at.
1349
- `subtext` (`Any`): String to insert.
1350
1351
**Returns:**
1352
- `str`: Modified string.
1353
1354
**Example:**
1355
```python { .api }
1356
from pydash import insert_substr
1357
1358
insert_substr('abcdef', 3, 'XYZ')
1359
# 'abcXYZdef'
1360
```
1361
1362
### join
1363
1364
```python { .api }
1365
def join(array: Iterable[Any], separator: Any = '') -> str
1366
```
1367
1368
Joins the elements in `array` into a string separated by `separator`.
1369
1370
**Parameters:**
1371
- `array` (`Iterable[Any]`): Array to join.
1372
- `separator` (`Any`): Separator string. Defaults to `''`.
1373
1374
**Returns:**
1375
- `str`: Joined string.
1376
1377
**Example:**
1378
```python { .api }
1379
from pydash import join
1380
1381
join(['a', 'b', 'c'], '~')
1382
# 'a~b~c'
1383
1384
join([1, 2, 3], ', ')
1385
# '1, 2, 3'
1386
```
1387
1388
### number_format
1389
1390
```python { .api }
1391
def number_format(number: Union[int, float], scale: int = 0, decimal_sep: str = '.', thousands_sep: str = ',') -> str
1392
```
1393
1394
Format a number to a readable string representation.
1395
1396
**Parameters:**
1397
- `number` (`Union[int, float]`): Number to format.
1398
- `scale` (`int`): Number of decimal places. Defaults to `0`.
1399
- `decimal_sep` (`str`): Decimal separator. Defaults to `'.'`.
1400
- `thousands_sep` (`str`): Thousands separator. Defaults to `','`.
1401
1402
**Returns:**
1403
- `str`: Formatted number string.
1404
1405
**Example:**
1406
```python { .api }
1407
from pydash import number_format
1408
1409
number_format(1234.5678)
1410
# '1,235'
1411
1412
number_format(1234.5678, 2)
1413
# '1,234.57'
1414
1415
number_format(1234.5678, 3, ',', ' ')
1416
# '1 234,568'
1417
```
1418
1419
### prune
1420
1421
```python { .api }
1422
def prune(text: Any, length: int = 0, omission: str = '...') -> str
1423
```
1424
1425
Like `truncate` except that it ensures that the pruned string doesn't exceed the original length and that words are not cut off unless necessary.
1426
1427
**Parameters:**
1428
- `text` (`Any`): String to prune.
1429
- `length` (`int`): Maximum length. Defaults to `0`.
1430
- `omission` (`str`): Omission string. Defaults to `'...'`.
1431
1432
**Returns:**
1433
- `str`: Pruned string.
1434
1435
**Example:**
1436
```python { .api }
1437
from pydash import prune
1438
1439
prune('hi-diddly-ho there, neighborino', 24)
1440
# 'hi-diddly-ho there...'
1441
```
1442
1443
### truncate
1444
1445
```python { .api }
1446
def truncate(text: Any, length: int = 30, omission: str = '...', separator: Any = None) -> str
1447
```
1448
1449
Truncates `text` if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".
1450
1451
**Parameters:**
1452
- `text` (`Any`): String to truncate.
1453
- `length` (`int`): Maximum string length. Defaults to `30`.
1454
- `omission` (`str`): Omission string. Defaults to `'...'`.
1455
- `separator` (`Any`, optional): Separator pattern to truncate to.
1456
1457
**Returns:**
1458
- `str`: Truncated string.
1459
1460
**Example:**
1461
```python { .api }
1462
from pydash import truncate
1463
1464
truncate('hi-diddly-ho there, neighborino')
1465
# 'hi-diddly-ho there, neighbo...'
1466
1467
truncate('hi-diddly-ho there, neighborino', 24)
1468
# 'hi-diddly-ho there, n...'
1469
```
1470
1471
### strip_tags
1472
1473
```python { .api }
1474
def strip_tags(text: Any) -> str
1475
```
1476
1477
Remove HTML/XML tags from `text`.
1478
1479
**Parameters:**
1480
- `text` (`Any`): String to strip tags from.
1481
1482
**Returns:**
1483
- `str`: String with tags removed.
1484
1485
**Example:**
1486
```python { .api }
1487
from pydash import strip_tags
1488
1489
strip_tags('<p>Hello <strong>world</strong>!</p>')
1490
# 'Hello world!'
1491
```
1492
1493
### slugify
1494
1495
```python { .api }
1496
def slugify(text: Any) -> str
1497
```
1498
1499
Convert `text` into an ASCII slug by replacing whitespace, dashes, and underscores with dashes, removing non-word characters, and converting to lower case.
1500
1501
**Parameters:**
1502
- `text` (`Any`): String to slugify.
1503
1504
**Returns:**
1505
- `str`: Slugified string.
1506
1507
**Example:**
1508
```python { .api }
1509
from pydash import slugify
1510
1511
slugify('Hello World!')
1512
# 'hello-world'
1513
1514
slugify(' Hello World ')
1515
# 'hello-world'
1516
```
1517
1518
### url
1519
1520
```python { .api }
1521
def url(text: Any, **params: Any) -> str
1522
```
1523
1524
Create a URL by appending query string parameters to `text`.
1525
1526
**Parameters:**
1527
- `text` (`Any`): Base URL string.
1528
- `params` (`**Any`): Query parameters to append.
1529
1530
**Returns:**
1531
- `str`: URL with query string.
1532
1533
**Example:**
1534
```python { .api }
1535
from pydash import url
1536
1537
url('http://example.com', foo='bar', hello='world')
1538
# 'http://example.com?foo=bar&hello=world'
1539
```
1540
1541
## String Replacement Functions
1542
1543
### replace
1544
1545
```python { .api }
1546
def replace(text: Any, pattern: Any, repl: Any, count: int = -1) -> str
1547
```
1548
1549
Replace occurrences of `pattern` with `repl` in `text`. Optionally, limit number of replacements to `count`.
1550
1551
**Parameters:**
1552
- `text` (`Any`): String to search and replace in.
1553
- `pattern` (`Any`): String pattern to find.
1554
- `repl` (`Any`): String to replace matches with.
1555
- `count` (`int`): Maximum number of replacements. Defaults to `-1` (all).
1556
1557
**Returns:**
1558
- `str`: String with replacements made.
1559
1560
**Example:**
1561
```python { .api }
1562
from pydash import replace
1563
1564
replace('aabbcc', 'b', 'X')
1565
# 'aaXXcc'
1566
1567
replace('aabbcc', 'b', 'X', 1)
1568
# 'aaXbcc'
1569
```
1570
1571
### reg_exp_replace
1572
1573
```python { .api }
1574
def reg_exp_replace(text: Any, pattern: Any, repl: Any) -> str
1575
```
1576
1577
Replace occurrences of regular expression `pattern` with `repl` in `text`.
1578
1579
**Parameters:**
1580
- `text` (`Any`): String to search and replace in.
1581
- `pattern` (`Any`): Regular expression pattern to find.
1582
- `repl` (`Any`): String to replace matches with.
1583
1584
**Returns:**
1585
- `str`: String with replacements made.
1586
1587
**Example:**
1588
```python { .api }
1589
from pydash import reg_exp_replace
1590
import re
1591
1592
reg_exp_replace('hello world', r'l+', 'L')
1593
# 'heLo worLd'
1594
```
1595
1596
### reg_exp_js_match
1597
1598
```python { .api }
1599
def reg_exp_js_match(text: Any, reg_exp: Any) -> Union[List[str], None]
1600
```
1601
1602
Return list of matches using JavaScript style regular expression.
1603
1604
**Parameters:**
1605
- `text` (`Any`): String to search.
1606
- `reg_exp` (`Any`): JavaScript style regular expression.
1607
1608
**Returns:**
1609
- `Union[List[str], None]`: List of matches or `None`.
1610
1611
### reg_exp_js_replace
1612
1613
```python { .api }
1614
def reg_exp_js_replace(text: Any, reg_exp: Any, repl: Any) -> str
1615
```
1616
1617
Replace text using JavaScript style regular expression.
1618
1619
**Parameters:**
1620
- `text` (`Any`): String to search and replace in.
1621
- `reg_exp` (`Any`): JavaScript style regular expression.
1622
- `repl` (`Any`): String to replace matches with.
1623
1624
**Returns:**
1625
- `str`: String with replacements made.
1626
1627
## String Utility Functions
1628
1629
### separator_case
1630
1631
```python { .api }
1632
def separator_case(text: Any, separator: Any) -> str
1633
```
1634
1635
Convert `text` to a delimited string where words are separated by `separator`.
1636
1637
**Parameters:**
1638
- `text` (`Any`): String to convert.
1639
- `separator` (`Any`): Separator to use between words.
1640
1641
**Returns:**
1642
- `str`: Converted string with custom separator.
1643
1644
**Example:**
1645
```python { .api }
1646
from pydash import separator_case
1647
1648
separator_case('fooBar', '.')
1649
# 'foo.Bar'
1650
1651
separator_case('Foo Bar', '_')
1652
# 'Foo_Bar'
1653
```
1654
1655
### series_phrase
1656
1657
```python { .api }
1658
def series_phrase(array: List[Any]) -> str
1659
```
1660
1661
Join array into a grammatically correct series phrase (e.g. "a, b, and c").
1662
1663
**Parameters:**
1664
- `array` (`List[Any]`): Array to join into series phrase.
1665
1666
**Returns:**
1667
- `str`: Grammatically correct series phrase.
1668
1669
**Example:**
1670
```python { .api }
1671
from pydash import series_phrase
1672
1673
series_phrase(['apple'])
1674
# 'apple'
1675
1676
series_phrase(['apple', 'banana'])
1677
# 'apple and banana'
1678
1679
series_phrase(['apple', 'banana', 'cherry'])
1680
# 'apple, banana, and cherry'
1681
```
1682
1683
### series_phrase_serial
1684
1685
```python { .api }
1686
def series_phrase_serial(array: List[Any]) -> str
1687
```
1688
1689
Like `series_phrase` but with serial comma (Oxford comma).
1690
1691
**Parameters:**
1692
- `array` (`List[Any]`): Array to join into series phrase.
1693
1694
**Returns:**
1695
- `str`: Series phrase with serial comma.
1696
1697
**Example:**
1698
```python { .api }
1699
from pydash import series_phrase_serial
1700
1701
series_phrase_serial(['apple', 'banana', 'cherry'])
1702
# 'apple, banana, and cherry'
1703
```
1704
1705
This Strings module provides comprehensive string manipulation capabilities with 66 functions covering all aspects of string processing from basic transformations to advanced formatting and pattern matching operations.