python substring before character code example

Example 1: how to fetch all chars of a string before a space in python

>>> s1.split(':')
['Username', ' How are you today?']
>>> s1.split(':')[0]
'Username'

Example 2: python regex get string before character

You don't need regex for this

>>> s = "Username: How are you today?"
You can use the split method to split the string on the ':' character

>>> s.split(':')
['Username', ' How are you today?']

Example 3: python extract all characters from string before a character

s1 = "Username: How are you today?"
>>> s1.split(':')
['Username', ' How are you today?']
>>> s1.split(':')[0]
'Username'