Programming (python) Flashcards
Multiple line strings (python)
””” or ‘’’
Single character from string
StringName[position]
Loop through characters in a string
iteration loop (for i in StringName and StringName[i])
Range of characters from string (python)
(StringName [position1:position2])
What points would (StringName [position1:position2]) take from a string?
From before position 1 to before position 2
Take a slice from first/last position of a string
(StringName[ :position2])
(StringName[position1: ])
Slice position relative to the end of a string (python)
StringName[position-2:position-1]
Rule for slice position relative to the end of a string
the last position in a string is -1, not -0
Concatenate strings
String1 + String2
Change format of another variable then concatenate into a placeholder (python)
String.format(Var1, Var2) where string contains placeholder marked {}
e.g:
txt2 = “My name is {0}, I’m {1}”.format(“John”,36)
Find length of string (python)
len(StringName)
Change string to all upper/lower case (python)
StringName.upper()
StringName.lower()
Remove whitespace from beginning/end of a string
StringName.strip()
Replace characters in a string
StringName.replace(“oldstring”, “newstring”)
Create a list of substrings by identifying a separator in a string (python)
StringName.split(“separator”)
e.g. ‘,’
Removes separator from all substrings