I find myself looking up certain things a little too often. Here are some tables that I hope you’ll find useful.
This table is a remix of the official documentation on precedence in Python 3, with the highest precedence operators at the top.
Operator | Description and examples |
---|---|
|
List/set/dict/generator creation or comprehension, parenthesized expression |
seq |
Index, slice, function call, attribute reference |
|
Exponentiation |
`+`x, `-`x, `~`x |
Positive, negative, bitwise |
|
Multiplication, float division, int division, remainder |
|
Addition, subtraction |
|
Bitwise left, right shifts |
|
Bitwise |
|
Bitwise |
|
Membership and equality tests |
|
Boolean (logical) |
|
Boolean |
|
Boolean |
|
Conditional expression |
|
|
Python offers both string methods (can be used with any str
object) and a string
module with some useful definitions. Let’s use these test variables:
>>>
s
=
"OH, my paws and whiskers!"
>>>
t
=
"I'm late!"
>>>
s
.
capitalize
()
'Oh, my paws and whiskers!'
>>>
s
.
lower
()
'oh, my paws and whiskers!'
>>>
s
.
swapcase
()
'oh, MY PAWS AND WHISKERS!'
>>>
s
.
title
()
'Oh, My Paws And Whiskers!'
>>>
s
.
upper
()
'OH, MY PAWS AND WHISKERS!'
>>>
s
.
count
(
'w'
)
2
>>>
s
.
find
(
'w'
)
9
>>>
s
.
index
(
'w'
)
9
>>>
s
.
rfind
(
'w'
)
16
>>>
s
.
rindex
(
'w'
)
16
>>>
s
.
startswith
(
'OH'
)
True
>>>
''
.
join
(
s
)
'OH, my paws and whiskers!'
>>>
' '
.
join
(
s
)
'O H , m y p a w s a n d w h i s k e r s !'
>>>
' '
.
join
((
s
,
t
))
"OH, my paws and whiskers! I'm late!"
>>>
s
.
lstrip
(
'HO'
)
', my paws and whiskers!'
>>>
s
.
replace
(
'H'
,
'MG'
)
'OMG, my paws and whiskers!'
>>>
s
.
rsplit
()
['OH,', 'my', 'paws', 'and', 'whiskers!']
>>>
s
.
rsplit
(
' '
,
1
)
['OH, my paws and', 'whiskers!']
>>>
s
.
split
()
['OH,', 'my', 'paws', 'and', 'whiskers!']
>>>
s
.
split
(
' '
)
['OH,', 'my', 'paws', 'and', 'whiskers!']
>>>
s
.
splitlines
()
['OH, my paws and whiskers!']
>>>
s
.
strip
()
'OH, my paws and whiskers!'
>>>
s
.
strip
(
's!'
)
'OH, my paws and whisker'
>>>
s
.
center
(
30
)
' OH, my paws and whiskers! '
>>>
s
.
expandtabs
()
'OH, my paws and whiskers!'
>>>
s
.
ljust
(
30
)
'OH, my paws and whiskers! '
>>>
s
.
rjust
(
30
)
' OH, my paws and whiskers!'
>>>
s
.
isalnum
()
False
>>>
s
.
isalpha
()
False
>>>
s
.
isprintable
()
True
>>>
s
.
istitle
()
False
>>>
s
.
isupper
()
False
>>>
s
.
isdecimal
()
False
>>>
s
.
isnumeric
()
False
These are class attributes that are used as constant definitions.
Attribute | Example |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This page intentionally left blank.
No, wait…it isn’t.
3.22.74.3