To get the substring between two specific characters in Python, you can use slicing with the indices of the characters.
In this example,
url
with a value of "https://example.com/page?query=1"
.'/'
and '?'
to get the substring between the characters.url = 'https://example.com/page?query=1'
start = url.index('/') + 1
end = url.index('?')
substring = url[start:end]
print('Substring between / and ?:', substring)
Substring between / and ?: /example.com/page
In this example,
str
with a value of "Hello,World!"
.','
and '!'
to get the substring between the characters.str = 'Hello,World!'
start = str.index(',') + 1
end = str.index('!')
substring = str[start:end]
print('Substring between , and !:', substring)
Substring between , and !: World
In this tutorial, we learned How to get Substring between two Specific Characters in Python language with well detailed examples.