Python Forum
YANQ: Yet Another Newbie Question !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
YANQ: Yet Another Newbie Question !
#2
Without knowing what service is, we should assume it's an object, and ref is just a property of that object.  We can fake it like so:
>>> class Service:
...   def __init__(self, tag):
...     self.ref = tag
...
>>> service = Service("4097:1:5:23")
>>> service.ref
'4097:1:5:23'
If that's true (...and it might not be), then we could take the fact that strings are immutable in python, and work with it directly...
>>> service.ref.startswith("4097:")
True
>>> new_tag = service.ref.replace("4097:", "1:", 1)
>>> service.ref
'4097:1:5:23'
>>> new_tag
'1:1:5:23'
Because strings are immutable, we have the new tag that we wanted (with 4097 replaced with 1), while service.ref is left unchanged.

Also, I added a count parameter to service.ref.replace, so that only the first 4097 found will be replaced.  That way, if the tag happens to look like "4097:1:4097:23", it won't be changed to "1:1:1:23".
Reply


Messages In This Thread
YANQ: Yet Another Newbie Question ! - by webdef - Aug-16-2017, 03:51 PM
RE: YANQ: Yet Another Newbie Question ! - by nilamo - Aug-16-2017, 04:39 PM
RE: YANQ: Yet Another Newbie Question ! - by webdef - Aug-17-2017, 07:18 AM
RE: YANQ: Yet Another Newbie Question ! - by nilamo - Aug-17-2017, 01:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 741 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 729 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 651 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 1,018 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,230 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Question from complete python's newbie Davicom 3 2,417 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Newbie question about running Python via GUI on OSX ejwjohn 8 3,630 Feb-05-2021, 03:20 PM
Last Post: Larz60+
  super newbie question: escape character tsavoSG 3 2,518 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  newbie question....importing a created class ridgerunnersjw 5 2,712 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Dumb newbie question JonEdward 5 3,332 Jul-22-2020, 10:06 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020