Strings are immutable, but they can be reassigned.
.upper()
- makes the string all UPPER case. e.g.
aString = 'This is the WAY'
print(aString.upper())
Will output the following:
THIS IS THE WAY
.lower()
- makes the string all lower case. e.g.
aString = 'This is the WAY'
print(aString.lower())
Will output the following:
this is the way
.split()
- splits the string at the specified character. e.g.
myString = "this/is/a/directory/structure"
print(myString.split('/'))
Will output a list of the elements:
['this', 'is', 'a', 'directory', 'structure']
Splits can also be chained together, for example:
myString = "this/is/a/directory/structure"
print(myString.split('/')[-1].split('t'))
Will give
['s', 'ruc', 'ure']
The [-1]
in the code above selects the last element of the first list created by the first split (i.e. the word 'structure') and then performs another split action on that, in this case splitting at the 't's and creating another list.
.format()
- replaces {} inside a string - can be indexed with numbers or keys*
*Alt method: f string literals print(f'{name} is {age})
where "name" and "age" are variables already defined, e.g.
name = "Jim"
age = 8
print(f'{name} is {age}')
will output:
Jim is 8
int (x, base=0)
Can be used to convert from a alternative counting method (for example base36) back to decimal (base10). This can be done like this:
tag = "HCC4SP2"
int(tag, base=36)
Will output:
37751273030