I just started with python, now I see myself needing the following, I have the following string:
1184-7380501-2023-183229
what i need is to trim this string and get only the following characters after the first hyphen. it should be as follows:
1184-738
how can i do this?
CodePudding user response:
s = "1184-7380501-2023-183229"
print(s[:8])
Or perhaps
import re
pattern = re.compile(r'^\d -...')
m = pattern.search(s)
print(m[0])
which accommodates variable length numeric prefixes.
CodePudding user response:
You could (you can do this a lot of different ways) use partition()
and join()
...
"".join([token[:3] if idx == 2 else token for idx, token in enumerate("1184-7380501-2023-183229".partition("-"))])