To get the substring before a specific character in Python, you can use string slicing.
In this example,
url
with a value of "https://example.com/page"
.find
method.url = 'https://example.com/page'
index = url.find('/')
substring = url[:index]
print('Substring before /:', substring)
Substring before /: https:
In this example,
str
with a value of "Hello,World"
.find
method.str = 'Hello,World'
index = str.find(',')
substring = str[:index]
print('Substring before ,:', substring)
Substring before ,: Hello
In this tutorial, we learned How to get Substring before Specific Character in Python language with well detailed examples.