' / '.join([
'first',
'second',
'third'
])
'first / second / third'
Split: reverse of join, that is, split a long string into smaller pieces by specifying a separator (default is a single space)
['this',
'is',
'a',
'long',
'string',
'with',
'11',
'parts',
'separated',
'by',
'space']
['we', 'can', 'also', 'split', 'with', 'other', 'characters']
the strip
methods remove spaces on both sides, or the the left/right:
These are python string methods to change the case of a string, often useful to compare strings while ignoring the case.
Casefold is like lower, but more agressive.
Comparing strings after folding the case.
Use casefold
instead of lower
for case-insensitive string comparison.
casefold
and lower
are mostly the same except in a few unicode characters:
# From https://stackoverflow.com/a/74702121/13430450
import sys
import unicodedata as ud
print("Unicode version:", ud.unidata_version, "\n")
total = 0
for codepoint in map(chr, range(sys.maxunicode)):
lower, casefold = codepoint.lower(), codepoint.casefold()
if lower != casefold:
total += 1
if total < 7: # only printing the first 7 examples that mismatch
for conversion, converted in zip(
("origin", "lower", "casefold"),
(codepoint, lower, casefold)
):
print(conversion, [ud.name(cp) for cp in converted], converted)
print()
print("Total differences:", total)
Unicode version: 13.0.0
origin ['MICRO SIGN'] µ
lower ['MICRO SIGN'] µ
casefold ['GREEK SMALL LETTER MU'] μ
origin ['LATIN SMALL LETTER SHARP S'] ß
lower ['LATIN SMALL LETTER SHARP S'] ß
casefold ['LATIN SMALL LETTER S', 'LATIN SMALL LETTER S'] ss
origin ['LATIN SMALL LETTER N PRECEDED BY APOSTROPHE'] ʼn
lower ['LATIN SMALL LETTER N PRECEDED BY APOSTROPHE'] ʼn
casefold ['MODIFIER LETTER APOSTROPHE', 'LATIN SMALL LETTER N'] ʼn
origin ['LATIN SMALL LETTER LONG S'] ſ
lower ['LATIN SMALL LETTER LONG S'] ſ
casefold ['LATIN SMALL LETTER S'] s
origin ['LATIN SMALL LETTER J WITH CARON'] ǰ
lower ['LATIN SMALL LETTER J WITH CARON'] ǰ
casefold ['LATIN SMALL LETTER J', 'COMBINING CARON'] ǰ
origin ['COMBINING GREEK YPOGEGRAMMENI'] ͅ
lower ['COMBINING GREEK YPOGEGRAMMENI'] ͅ
casefold ['GREEK SMALL LETTER IOTA'] ι
Total differences: 297
Method that returns a boolean
This can be chained with casefold
for case-insensitive comparisons:
We may further contain substrings (more than 1 characters:)
'954:597 Data Wrangling and Husbandry (3)'
'16:954:597 Data Wrangling and Husbandry (3)'
Here we can a few example of format strings. Format strings are of the form f'something {variable}
, prefixing the first quote with f. It will replace the curly bracket by the variable.
It also works with lists:
as well as dictionaries:
'Results of the 2023 some referendum'
We may further use methods inside the format string: