Strings [immutable, iterable]
|
"hello world" |
string = "hello world".capitalize()
|
"Hello world" |
string = "Hello World".lower()
|
"hello world" |
string = "Hello World".upper()
|
"HELLO WORLD" |
string = "hello world".title()
|
"Hello Word" |
string = "Hello wOrld".swapcase()
|
"hELLO WoRLD" |
string = "ß".casefold()
|
"ss" |
.casefold()
is a more aggressive .lower()
used for string comparisons. |
int = "abaaba ababa".count("aba")
|
3 |
"ababa" is counted to have 1 "aba" |
int = "acadacad".find("cad")
|
1 |
int = "acadacad".rfind("cad")
|
5 |
string.find(value, start, end)
finds the first occurrence of the value after start before end. |
str.find(str2) == str.index(str2)
, except for when str2 isn't found .find()
returns 0 while .index()
throws an error |
string = "ab ab aa ab ab".replace("ab", "x", 3)
|
"x x aa x ab" |
str.replace(oldvalue, newvalue, count)
|
replaces count of oldvalues in the string with newvalues. Default count is alll. |
|
bool = "_,_._ban_ana__..,".strip("_,.")
|
"ban_ana" |
bool = "_,_._ban_ana__..,".lstrip("_,.")
|
"ban_ana__..," |
bool = "_,_._ban_ana__..,".rstrip("_,.")
|
"ban_ana__..," |
string = "hello".center(10, '.')
|
"..hello..." |
str.center(int, char)
returns a string of int characters, with string in the middle and chars on the front and the end. |
|
True |
|
True |
bool = "-12".isnumeric()
|
False |
bool = "1.2".isdigit()
|
False |
bool = "aba".endswith("ba")
|
True |
bool = "aba".startswith("ab")
|
True |
bool = "a12".islower()
|
True |
bool = "A12".isupper()
|
True |
`list = "Master Lordern".split("er") |
['Mast', ' Lord', 'n'] |
string = "-10.2".zfill(7)
|
"-0010.2" |
.zfill()
fills the beginning of the string with zeroes until the specified amount of characters are taken up, -, + and . included. Unlike .rjust(), .zfill() puts 0s after - and + signs. |
string = "hello world".title()
|
"Hello Word" |
string = "Hello wOrld".swapcase()
|
"hELLO WoRLD" |
string = "hello world"[2:7] string slicing |
"llo w" |
All string methods return a new string without mutating the original string
Lists [mutable, iterable]
|
[] |
list = list() |
[] |
|
[1, 2, 3] |
|
[1, 2, 3, 4] |
|
[1, 8, 2, 3, 4] |
list.insert(ind, elem)
inserts elem at position ind |
list.extend([8, 9, 1, 4, 3])
|
[1, 8, 2, 3, 4, 8, 9, 1, 4, 3] |
list.extend(<iterable>)
extends the list by the elements of the iterable |
|
[1, 8, 2, 3, 4, 8, 9, 1, 3] |
|
[8, 2, 3, 4, 8, 9, 1, 3] |
list.pop(ind)
pops the element at position ind |
|
[2, 3, 4, 8, 9, 1, 3] |
list.remove(elem)
removes the first instance of elem |
|
1 |
list.index(elem) returns the index of the first elem |
list.index() does not mutate the original list |
|
2 |
list.count(elem) returns the count of elements with value elem |
list.count() does not mutate the original list |
|
[1, 2, 3, 3, 4 ,8, 9] |
list.sort()
cannot sort int against str |
|
[9, 8, 4, 3, 3, 2, 1] |
list slicing: list2 = list[1:5]
|
[8, 4, 3, 3] |
|
list2 is [9, 8, 4, 3, 3, 2, 1] |
list slicing or list.copy() do not mutate the original list, and create shallow copies. |
|
[] |
Tuples [immutable, iterable]
|
int = ("a", "a", "ab").count('a')
|
2 |
int = ("a", "a", "ab").index('a')
|
0 |
.index()
returns the position of the first instance |
Dictionaries [mutable, iterable]
|
{} |
|
{} |
dictionary = {key1: value1, key2: value2}
|
{key1: value1, key2: value2} |
dictionary["str1"] = "str2"
|
{key1: value1, key2: value2, 'str1': 'str2'} |
dictionary.pop('str1')
|
{key1: value1, key2: value2} |
|
{key1: value1} |
.popitem()
removes the last added element |
dictionary2 = {key2: value2} |
dictionary.update(dictionary2) |
{key1: value1, key2: value2} |
.update(dict) updates the dictionary with the key:value pairs of dict. In case of a conflict, dict is prioritized. |
list = dictionary.keys()
|
[key1, key2] |
list = dictionary.values()
|
[value1, value2] |
list = dictionary.items()
|
[(key1, value1), (key2, value2)] |
.items()
returns a list of tuples |
something = dictionary.get(keyname, value)
|
if keyname in a dictionary does not exist as a key, value is returned. |
dictionary2 = dictionary.copy()
|
{key1: value1, key2: value2} |
|
{} |
Functions
def function(parameter1, parameter2 = defaultvalue):
<something>
return value
function(argument1, argument2)
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment