aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Shom770 <[email protected]>2021-12-29 13:37:56 -0500
committerGravatar GitHub <[email protected]>2021-12-29 18:37:56 +0000
commit548766959abc77ffc9140ec4f5be52cfcdff3a6c (patch)
treef02829232d40f132ef04c24d3e76aa436deff292
parentInclude message counts in all channels (#2016) (diff)
Strip gotcha tag (PR #2000)
* adding strip-gotcha tag
-rw-r--r--bot/resources/tags/strip-gotcha.md17
1 files changed, 17 insertions, 0 deletions
diff --git a/bot/resources/tags/strip-gotcha.md b/bot/resources/tags/strip-gotcha.md
new file mode 100644
index 000000000..9ad495cd2
--- /dev/null
+++ b/bot/resources/tags/strip-gotcha.md
@@ -0,0 +1,17 @@
+When working with `strip`, `lstrip`, or `rstrip`, you might think that this would be the case:
+```py
+>>> "Monty Python".rstrip(" Python")
+"Monty"
+```
+While this seems intuitive, it would actually result in:
+```py
+"M"
+```
+as Python interprets the argument to these functions as a set of characters rather than a substring.
+
+If you want to remove a prefix/suffix from a string, `str.removeprefix` and `str.removesuffix` are recommended and were added in 3.9.
+```py
+>>> "Monty Python".removesuffix(" Python")
+"Monty"
+```
+See the documentation of [str.removeprefix](https://docs.python.org/3.10/library/stdtypes.html#str.removeprefix) and [str.removesuffix](https://docs.python.org/3.10/library/stdtypes.html#str.removesuffix) for more information.