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

3

2 Answers 2

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'
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.