'How to find second occurence of specified value from user input and return the values in between
Currently trying to write a script that takes a user input that has multiple numbers, a hypen, then a string of letters, another hyphen and some numbers. What I'm trying to do is display the values between the two hyphens.
Here's what I have so far
serialNum = input("Input the serial code of the product please")
hyphenIndex = serialNum.find("-")
print("The color of the product is ", serialNum[hyphenIndex:])
Solution 1:[1]
If you always have the input between hyphens, the most simple thing you can do is splitting on the hyphen and get the second value:
serialNum = "34783-red-348943"
print("The color of the product is ", serialNum.split("-")[1])
Does this solution work for you?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lemon |
