How would I get everything before a : in a string Python

前端 未结 5 1613
囚心锁ツ
囚心锁ツ 2020-11-30 20:45

I am looking for a way to get all of the letters in a string before a : but I have no idea on where to start. Would I use regex? If so how?

string = \"Userna         


        
5条回答
  •  感情败类
    2020-11-30 21:42

    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'
    

提交回复
热议问题