#๐Ÿ”’ Replacing numbers

22 messages ยท Page 1 of 1 (latest)

austere robin
#

Does anyone know what code to use when you need to replace a certain number of digits/numbers in a string?

torn flaxBOT
#

@austere robin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

grave anvil
#

Heya @austere robin ๐Ÿ‘‹
You're probably looking at re.sub() or str.replace() (renamed as Reptile's examples are better :P)
If you can give some examples of what needs to be done..?

spare grove
#

Uh, depends on what you mean by "replace a certain number of digits/numbers", but str.replace and re.sub come to mind.

austere robin
#

I've been trying to use something like this: r = r.replace([:S])

jaunty tulip
spare grove
#

I'm not sure what that's meant to mean, but replace has an argument to limit the max number of replacements, if that's what you want.

grave anvil
#

!d str.replace

torn flaxBOT
#

str.replace(old, new[, count])```
Return a copy of the string with all occurrences of substring *old* replaced by *new*. If the optional argument *count* is given, only the first *count* occurrences are replaced.
jaunty tulip
#

I don't think replace is actually what you want

#

You are just trying to remove a beginning part?

austere robin
jaunty tulip
#

Flip your slice around

#

And assign that slice back to your variable

austere robin
#

like this? R1 = slice(r[:S])

jaunty tulip
#

The colon notation in [] is a slice (when used as getitem)

#

And you need to flip it around: the stop value should be the start value

austere robin
#

so like this? r = r.replace([S:])

jaunty tulip
#

Try to not guess

torn flaxBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.