Python Forum
split string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: split string (/thread-26970.html)



split string - enigma619 - May-20-2020

Hi

I've a question about proper method to split a string.

my strings are like this:

object Host "serviceXXX-YYYY" {
I'm doing my plit like this:

hostname = line.split(" ")[2].split('"')[1]
Do you have a better method to do this split?
Thanks for your help

Alex


RE: split string - perfringo - May-20-2020

Alternatively you can do only one split:

>>> s = 'object Host "serviceXXX-YYYY" {'
>>> s.split('"', maxsplit=2)[1]                                                 
'serviceXXX-YYYY'