0

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

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
zoit
  • 617
  • 2
  • 20
  • 41
  • 1
    weird, it work in my Python 3.6.9 >>> s = 'http://pbs.twimg.com/media/xxiajdiojadf.jpg||2013-11-17,16:19:52||more text in this string' >>> s 'http://pbs.twimg.com/media/xxiajdiojadf.jpg||2013-11-17,16:19:52||more text in this string' >>> s_list = s.split('||') >>> s_list ['http://pbs.twimg.com/media/xxiajdiojadf.jpg', '2013-11-17,16:19:52', 'more text in this string'] >>> s_list[0] 'http://pbs.twimg.com/media/xxiajdiojadf.jpg' >>> s_list[1] '2013-11-17,16:19:52' >>> s_list[2] 'more text in this string' – Hoxmot Jun 12 '20 at 11:09
  • what you tried should work – Atharva Kadlag Jun 12 '20 at 11:10
  • @zoit can you please enter the error and python version as well? – Harsha Biyani Jun 12 '20 at 11:13

2 Answers2

1

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'
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

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

Alterlife
  • 6,557
  • 7
  • 36
  • 49