-3

I am working on something in Python that reads a file and outputs some text. Let's say that I have this string:

a = "hello // world"

Using this function, I would remove the part of the string that comes after the double slash

def magic(input):
    return "hello "

How could I achieve this simply in python? (Basically I want to remove everything after the //)

3 Answers3

1

Regular expressions are often a lot of trouble, but for certain problems they're perfect.

>>> import re
>>> re.sub('//.*', '', 'hello // world')
'hello '
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

Use find() to get the position of the double slashes. Then take a slice of the original string up to that position.

a = "hello // world"
pos = a.find("//")
return a[:pos]
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0
>>> 'hello//world'.split('//')[0]
'hello'
>>>
mhega
  • 57
  • 5