Skilo Skilo
Skilo Skilo

Reputation: 526

Need help separating the contents of a variable into 2 varaibles in python

So i have a string variable that looks like this:

text = "('127.0.0.1', 64069)"

I want to separate the ip address into a variable called HOST and the port into another variable called PORT.

I tried this to clean up the unessisary chars:

x = text.replace(',', '').replace('(', '').replace(')', '').replace("'", "")

It's sloppy looking but it works, Right now im just stuck trying to figure out how to go about separating the IP and the PORT into 2 separate variables.

Upvotes: 0

Views: 28

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799044

>>> ast.literal_eval(text)
('127.0.0.1', 64069)
>>> host, port = ast.literal_eval(text)
>>> host
'127.0.0.1'
>>> port
64069

Upvotes: 3

Related Questions