Python split() To Create List From String

In previous post I already write about list and join to create list or array and you can use join to convert list to be a string. Now if you will do opposite create list from string data you can use split()

se I had create variable cmd contain string data
>>> cmd = 'show cdp neighbor, show ip int brief, show ip route'
>>> cmd
'show cdp neighbor, show ip int brief, show ip route'

and I make it as a list in new variable called cmd_list using comma (,) to separate each data.
>>> cmd_list = cmd.split(',')
>>> cmd_list
['show cdp neighbor', ' show ip int brief', ' show ip route']


You can also print the data in a list one by one using sequence number
>>> interfaces=['gig1/1', 'gig1/2', 'gig1/3', 'gig1/4', 'gig1/5']
>>>
>>> print interfaces
['gig1/1', 'gig1/2', 'gig1/3', 'gig1/4', 'gig1/5']
>>>
>>>
>>> interfaces[0]
'gig1/1'
>>> interfaces[1]
'gig1/2'
>>> interfaces[2]
'gig1/3'
>>> interfaces[3]
'gig1/4'
>>> interfaces[4]
'gig1/5'

If you want to count how many element or deta in your list you can use len()
>>> len(interfaces)
5

Tulis komentar anda... Conversion Conversion Emoticon Emoticon

Thanks for your comment