Python Forum

Full Version: split string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Alternatively you can do only one split:

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