Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading