zoit
zoit

Reputation: 667

Get a part of string with python

I am working with strings in python. I have a string like this one:

'http://pbs.twimg.com/media/xxiajdiojadf.jpg||2013-11-17,16:19:52||more text in this string'

To get the first and the second part of the string is easy, but what I need to do for the second part?, I mean I want to get the text after the second || . For the first ones:

url=s.split("||")[0] and  date=s.split("||")[1]

I have try with url=s.split("||")[2] but I have nothing Thanks in advance

Upvotes: 0

Views: 64

Answers (2)

Alterlife
Alterlife

Reputation: 6625

I think you have just made a typo. You logic is correct. On your third line you should use another variable, not 'url'.

A bit more terse way to do it would be:

url, date, descr = s.split("||")

https://repl.it/repls/WrongTinyCylinder#main.py

Upvotes: 0

Harsha Biyani
Harsha Biyani

Reputation: 7268

You can get that using 2nd index:

s.split("||")[2]

output:

'more text in this string'

split will return the list.

>>> s.split("||")
['http://pbs.twimg.com/media/xxiajdiojadf.jpg', '2013-11-17,16:19:52', 'more text in this string']
>>> url,date,extra = s.split("||")
>>> print(url)
'http://pbs.twimg.com/media/xxiajdiojadf.jpg'
>>> print(date)
'2013-11-17,16:19:52'
>>> print(extra)
'more text in this string'

Upvotes: 1

Related Questions