Just use the split function. It returns a list, so you can keep the first element:

>>> s1.split(':')
['Username', ' How are you today?']
>>> s1.split(':')[0]
'Username'
Answer from fredtantini on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-prefix-extraction-before-specific-character
Prefix Extraction Before Specific Character - Python - GeeksforGeeks
July 12, 2025 - One efficient way to achieve this is by using the find() method, which is optimal for prefix extraction due to its simplicity and efficiency. ... i = s.find(c) searches for the first occurrence of the character "r" in the string s . As it returns ...
Discussions

regex - Python search / extract string before and after a character - Stack Overflow
Need help in extracting string before and after a character using regex in python string = "My City | August 5" I would like to extract "My City" and extract "August 5" string1 = "My City" string... More on stackoverflow.com
🌐 stackoverflow.com
python - Extract the word before a specific character - Stack Overflow
from this string i want to extract the variable and the parameter i.e. processdata(Message_handler_t* ) how can I do it using python and regex. I tried this but it also extracts the word before the variable More on stackoverflow.com
🌐 stackoverflow.com
python - how to get the last part of a string before a certain character? - Stack Overflow
I am trying to print the last part of a string before a certain character. I'm not quite sure whether to use the string .split() method or string slicing or maybe something else. Here is some More on stackoverflow.com
🌐 stackoverflow.com
python - Extract string before specific character - Stack Overflow
i have list of images , which are named as follow : Abyssinian_1.jpg so name_digit.jpg of course if it would be only one _digit.jpg, then using split statement it is very easy,but whe have also... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Note.nkmk.me
note.nkmk.me › home › python
Extract a Substring from a String in Python (Position, Regex) | note.nkmk.me
April 29, 2025 - The built-in len() function returns the number of characters in a string. You can use it to get the central character or extract the first or second half of a string by slicing.
🌐
TutorialsPoint
tutorialspoint.com › article › python-prefix-extraction-before-specific-character
Python - Prefix extraction before specific character
September 1, 2023 - def extract_prefix(text, delimiter): if delimiter in text: return text.split(delimiter)[0] else: return text # Return original if delimiter not found # Test with different cases test_cases = ["hello-world", "no_delimiter", "start:middle:end"] ...
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-19.php
Python: Get the last part of a string before a specified character - w3resource
June 12, 2025 - Then, print the result. ...com/python-exercises' # Use the rsplit() method with '-' as the separator to split the string from the right, # and [0] to get the part before the last '-' character....
🌐
EyeHunts
tutorial.eyehunts.com › home › python cut string before character | example code
Python cut string before character | Example code - EyeHunts
October 28, 2021 - Simple example code cut all chars of a string before a “and” in python. Just use the split function. It returns a list, and keep the first element: s = "Python and data science" res = s.split("and")[0] print(res) ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-till-substring
Python - String till Substring - GeeksforGeeks
July 12, 2025 - The split() method is a simple and efficient way to extract the part of a string before a specific substring. By splitting the string at the substring and taking the first part, we can achieve the desired result.
Find elsewhere
🌐
AskPython
askpython.com › python › examples › extracting-text-before-colon-python-regex
How to Extract Text Before a Colon (:) Using Regex in Python? - AskPython
May 31, 2023 - In Python, you can use the ‘re’ module’s functions – split(), search(), and span() – to extract everything before a colon in a string. The split() function splits the string at the colon, search() finds the colon in the string, and ...
🌐
Stack Overflow
stackoverflow.com › questions › 63190458 › extract-the-word-before-a-specific-character › 63190681
python - Extract the word before a specific character - Stack Overflow
from this string i want to extract the variable and the parameter i.e. processdata(Message_handler_t* ) how can I do it using python and regex. I tried this but it also extracts the word before the variable
🌐
Tutorial Gateway
tutorialgateway.org › python-substring
Python substring
March 26, 2025 - character within a string. Next, you can use this Python string slicing to return a text before or after a character.
🌐
H2K Infosys
h2kinfosys.com › blog › how to extract a string between two characters in python
How to Extract a String Between Two Characters in Python
December 18, 2025 - Python’s string methods like find() offer a straightforward way to locate and extract substrings. Always validate the presence of characters before attempting to slice strings to avoid errors.
🌐
Finxter
blog.finxter.com › home › learn python blog › python | split string until character/substring
Python | Split String until Character/Substring - Be on the Right Side of Change
December 15, 2022 - In the above solutions, it finds and groups all the characters until “Boy” in the first case and “/” in the second case. ... The partition() method searches for a separator substring and returns a tuple with three strings: (1) everything before the separator, (2) the separator itself, and (3) everything after it.
Top answer
1 of 2
135

You are looking for str.rsplit(), with a limit:

print x.rsplit('-', 1)[0]

.rsplit() searches for the splitting string from the end of input string, and the second argument limits how many times it'll split to just once.

Another option is to use str.rpartition(), which will only ever split just once:

print x.rpartition('-')[0]

For splitting just once, str.rpartition() is the faster method as well; if you need to split more than once you can only use str.rsplit().

Demo:

>>> x = 'http://test.com/lalala-134'
>>> print x.rsplit('-', 1)[0]
http://test.com/lalala
>>> 'something-with-a-lot-of-dashes'.rsplit('-', 1)[0]
'something-with-a-lot-of'

and the same with str.rpartition()

>>> print x.rpartition('-')[0]
http://test.com/lalala
>>> 'something-with-a-lot-of-dashes'.rpartition('-')[0]
'something-with-a-lot-of'
2 of 2
6

Difference between split and partition is split returns the list without delimiter and will split where ever it gets delimiter in string i.e.

x = 'http://test.com/lalala-134-431'

a,b,c = x.split(-)
print(a)
"http://test.com/lalala"
print(b)
"134"
print(c)
"431"

and partition will divide the string with only first delimiter and will only return 3 values in list

x = 'http://test.com/lalala-134-431'
a,b,c = x.partition('-')
print(a)
"http://test.com/lalala"
print(b)
"-"
print(c)
"134-431"

so as you want last value you can use rpartition it works in same way but it will find delimiter from end of string

x = 'http://test.com/lalala-134-431'
a,b,c = x.rpartition('-')
print(a)
"http://test.com/lalala-134"
print(b)
"-"
print(c)
"431"
🌐
Python Forum
python-forum.io › thread-41081.html
extract substring from a string before a word !!
November 7, 2023 - hello all ... this is my output : Output:{'status': True, 'msg': 'Success', 'data': [{'title': 'perrier'}, {'title': 'Polo'}, {'title': 'Purina'}, {'title': 'Pizza Hut'}, {'title': 'Pepsi'}, {'title': 'Pope1'}, {'title': 'Pantene'}, {'title': 'P&am......
🌐
Linux Hint
linuxhint.com › extract-substring-regex-python
Linux Hint – Linux Hint
March 20, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
CodeBasics
code-basics.com › programming › python course › extracting characters from a string
Extracting characters from a string | Python | CodeBasics
If you only want to get a few characters from an expression, you don't need to write a large number of lines of code, just extract the element using an index. You can also use a negative index to make it easier to output characters from the end of an expression. Next, let's see how this knowledge can be used to extract a substring from a string.
🌐
Sentry
sentry.io › sentry answers › python › extract a substring from a string in python
Extract a substring from a string in Python
2 weeks ago - We can extract a substring from a string using Python’s slice notation. The syntax is as follows: ... The variable substring will include all characters in the string, starting at the start index up to but not including the end index. Strings in Python are 0-indexed, so we must count characters from 0 to the length of the string minus 1.