r/pythontips • • Sep 23 '21

Data_Science 15 Must-Know Python String Methods

Python is a great language. It is relatively easy to learn and has an intuitive syntax. The rich selection of libraries also contribute to the popularity and success of Python. Visit here: https://www.thinkdataanalytics.com/python-string-methods/

41 Upvotes

5 comments sorted by

View all comments

3

u/expressadmin Sep 23 '21

Would somebody give an example as to when you would want to use partition? I've heard of it before but never seen it used and struggle to think of a use case where I would use it.

2

u/oohay_email2004 Sep 24 '21

I was hitting an FTP server for file modification times and it didn't support the newer way to get them. str.partition is one way to parse the response, could also go with str.split(' ', maxsplit=1)

From memory:

response = ftp.voidcmd(f'MDTM {filename}')
code, _, string = response.partition(' ')
if code != '213':
    # do stuff with error, `string` is an 
    # error message that may contain spaces.
else:
    # parse `string` as datetime and continue