python substring until 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: get string until character python

s = "first_second"
s.split('_')[0] # Split the string on '_' return the first part. 
>>> 'first'

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'

Tags:

Cpp Example