---
source: crates/ruff_linter/src/rules/refurb/mod.rs
---
FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
  --> FURB188.py:7:5
   |
 6 |   def remove_extension_via_slice(filename: str) -> str:
 7 | /     if filename.endswith(".txt"):
 8 | |         filename = filename[:-4]
   | |________________________________^
 9 |
10 |       return filename
   |
help: Use removesuffix instead of assignment conditional upon endswith.
  |
6 | def remove_extension_via_slice(filename: str) -> str:
  -     if filename.endswith(".txt"):
  -         filename = filename[:-4]
7 +     filename = filename.removesuffix(".txt")
8 |
  |

FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
  --> FURB188.py:14:5
   |
13 |   def remove_extension_via_slice_len(filename: str, extension: str) -> str:
14 | /     if filename.endswith(extension):
15 | |         filename = filename[:-len(extension)]
   | |_____________________________________________^
16 |
17 |       return filename
   |
help: Use removesuffix instead of assignment conditional upon endswith.
   |
13 | def remove_extension_via_slice_len(filename: str, extension: str) -> str:
   -     if filename.endswith(extension):
   -         filename = filename[:-len(extension)]
14 +     filename = filename.removesuffix(extension)
15 |
   |

FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
  --> FURB188.py:21:12
   |
20 | def remove_extension_via_ternary(filename: str) -> str:
21 |     return filename[:-4] if filename.endswith(".txt") else filename
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Use removesuffix instead of ternary expression conditional upon endswith.
   |
20 | def remove_extension_via_ternary(filename: str) -> str:
   -     return filename[:-4] if filename.endswith(".txt") else filename
21 +     return filename.removesuffix(".txt")
22 |
   |

FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
  --> FURB188.py:25:12
   |
24 | def remove_extension_via_ternary_with_len(filename: str, extension: str) -> str:
25 |     return filename[:-len(extension)] if filename.endswith(extension) else filename
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Use removesuffix instead of ternary expression conditional upon endswith.
   |
24 | def remove_extension_via_ternary_with_len(filename: str, extension: str) -> str:
   -     return filename[:-len(extension)] if filename.endswith(extension) else filename
25 +     return filename.removesuffix(extension)
26 |
   |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
  --> FURB188.py:29:12
   |
28 | def remove_prefix(filename: str) -> str:
29 |     return filename[4:] if filename.startswith("abc-") else filename
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Use removeprefix instead of ternary expression conditional upon startswith.
   |
28 | def remove_prefix(filename: str) -> str:
   -     return filename[4:] if filename.startswith("abc-") else filename
29 +     return filename.removeprefix("abc-")
30 |
   |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
  --> FURB188.py:33:12
   |
32 | def remove_prefix_via_len(filename: str, prefix: str) -> str:
33 |     return filename[len(prefix):] if filename.startswith(prefix) else filename
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Use removeprefix instead of ternary expression conditional upon startswith.
   |
32 | def remove_prefix_via_len(filename: str, prefix: str) -> str:
   -     return filename[len(prefix):] if filename.startswith(prefix) else filename
33 +     return filename.removeprefix(prefix)
34 |
   |

FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
   --> FURB188.py:146:9
    |
144 |     SUFFIX = "suffix"
145 |
146 |     x = foo.bar.baz[:-len(SUFFIX)] if foo.bar.baz.endswith(SUFFIX) else foo.bar.baz
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |
148 | def remove_prefix_comparable_literal_expr() -> None:
    |
help: Use removesuffix instead of ternary expression conditional upon endswith.
    |
145 |
    -     x = foo.bar.baz[:-len(SUFFIX)] if foo.bar.baz.endswith(SUFFIX) else foo.bar.baz
146 +     x = foo.bar.baz.removesuffix(SUFFIX)
147 |
    |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
   --> FURB188.py:149:12
    |
148 | def remove_prefix_comparable_literal_expr() -> None:
149 |     return ("abc" "def")[3:] if ("abc" "def").startswith("abc") else "abc" "def"
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
150 |
151 | def shadow_builtins(filename: str, extension: str) -> None:
    |
help: Use removeprefix instead of ternary expression conditional upon startswith.
    |
148 | def remove_prefix_comparable_literal_expr() -> None:
    -     return ("abc" "def")[3:] if ("abc" "def").startswith("abc") else "abc" "def"
149 +     return "abc" "def".removeprefix("abc")
150 |
    |

FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
   --> FURB188.py:154:12
    |
152 |     from builtins import len as builtins_len
153 |
154 |     return filename[:-builtins_len(extension)] if filename.endswith(extension) else filename
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155 |
156 | def okay_steps():
    |
help: Use removesuffix instead of ternary expression conditional upon endswith.
    |
153 |
    -     return filename[:-builtins_len(extension)] if filename.endswith(extension) else filename
154 +     return filename.removesuffix(extension)
155 |
    |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
   --> FURB188.py:158:5
    |
156 |   def okay_steps():
157 |       text = "!x!y!z"
158 | /     if text.startswith("!"):
159 | |         text = text[1::1]
    | |_________________________^
160 |       if text.startswith("!"):
161 |           text = text[1::True]
    |
help: Use removeprefix instead of assignment conditional upon startswith.
    |
157 |     text = "!x!y!z"
    -     if text.startswith("!"):
    -         text = text[1::1]
158 +     text = text.removeprefix("!")
159 |     if text.startswith("!"):
    |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
   --> FURB188.py:160:5
    |
158 |       if text.startswith("!"):
159 |           text = text[1::1]
160 | /     if text.startswith("!"):
161 | |         text = text[1::True]
    | |____________________________^
162 |       if text.startswith("!"):
163 |           text = text[1::None]
    |
help: Use removeprefix instead of assignment conditional upon startswith.
    |
159 |         text = text[1::1]
    -     if text.startswith("!"):
    -         text = text[1::True]
160 +     text = text.removeprefix("!")
161 |     if text.startswith("!"):
    |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
   --> FURB188.py:162:5
    |
160 |       if text.startswith("!"):
161 |           text = text[1::True]
162 | /     if text.startswith("!"):
163 | |         text = text[1::None]
    | |____________________________^
164 |       print(text)
    |
help: Use removeprefix instead of assignment conditional upon startswith.
    |
161 |         text = text[1::True]
    -     if text.startswith("!"):
    -         text = text[1::None]
162 +     text = text.removeprefix("!")
163 |     print(text)
    |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
   --> FURB188.py:183:5
    |
181 |       # with fix `text = text.removeprefix("ř")`
182 |       text = "řetězec"
183 | /     if text.startswith("ř"): 
184 | |         text = text[1:]
    | |_______________________^
    |
help: Use removeprefix instead of assignment conditional upon startswith.
    |
182 |     text = "řetězec"
    -     if text.startswith("ř"): 
    -         text = text[1:]
183 +     text = text.removeprefix("ř")
184 |
    |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
   --> FURB188.py:190:5
    |
188 |       # should be linted
189 |       text = "\ud800\udc00heythere"
190 | /     if text.startswith("\ud800\udc00"):
191 | |         text = text[2:]
    | |_______________________^
192 |       text = "\U00010000heythere"
193 |       if text.startswith("\U00010000"):
    |
help: Use removeprefix instead of assignment conditional upon startswith.
    |
189 |     text = "\ud800\udc00heythere"
    -     if text.startswith("\ud800\udc00"):
    -         text = text[2:]
190 +     text = text.removeprefix("\ud800\udc00")
191 |     text = "\U00010000heythere"
    |

FURB188 [*] Prefer `str.removeprefix()` over conditionally replacing with slice.
   --> FURB188.py:193:5
    |
191 |           text = text[2:]
192 |       text = "\U00010000heythere"
193 | /     if text.startswith("\U00010000"):
194 | |         text = text[1:]
    | |_______________________^
195 |       
196 |       # should not be linted
    |
help: Use removeprefix instead of assignment conditional upon startswith.
    |
192 |     text = "\U00010000heythere"
    -     if text.startswith("\U00010000"):
    -         text = text[1:]
193 +     text = text.removeprefix("\U00010000")
194 |     
    |

FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
   --> FURB188.py:205:5
    |
203 |   def func():
204 |       a = "sjdfaskldjfakljklfoo"
205 | /     if a.endswith("foo"):
206 | |         a = a[: -len("foo")]
    | |____________________________^
207 |       print(a)
    |
help: Use removesuffix instead of assignment conditional upon endswith.
    |
204 |     a = "sjdfaskldjfakljklfoo"
    -     if a.endswith("foo"):
    -         a = a[: -len("foo")]
205 +     a = a.removesuffix("foo")
206 |     print(a)
    |

FURB188 [*] Prefer `str.removesuffix()` over conditionally replacing with slice.
   --> FURB188.py:212:9
    |
210 |   def example(filename: str, text: str):
211 |       filename = (
212 | /         filename[:-4]  # text
213 | |         if filename.endswith(".txt")  # text
214 | |         else filename
    | |_____________________^
215 |       )
    |
help: Use removesuffix instead of ternary expression conditional upon endswith.
    |
211 |     filename = (
    -         filename[:-4]  # text
    -         if filename.endswith(".txt")  # text
    -         else filename
212 +         filename.removesuffix(".txt")
213 |     )
    |
note: This is an unsafe fix and may change runtime behavior
