BernardL BernardL - 7 months ago 43
Python Question

Return substring if contains character

Given a string, "Bajsd 2-478 1278123" and string similar to that. Is there a method to pull only the substring which contains "-"?

So in this case, it would be, 2-478. The length of the entire string and the substring containing "-" can vary.


Just split the string and print the item if it contains "-"

print "".join([x for x in "Bajsd 2-478 1278123".split(" ") if "-" in x])