Questions about Python Split

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

#Python 300 for beginners
The question
#27
Print the domain from the web page address stored in url.

>> url = "http://sharebook.kr"
Example of an execution:
kr
Original answer
url = "http://sharebook.kr"
url_split = url.split('.')
print(url_split[-1])

The answer I wrote
url = "http://sharebook.kr"
print(url[-2:])

The question I want to ask

url = "http://sharebook.kr"
url_split = url.split('o')
print(url_split[1])

This will result in no values.

Why doesn't the result come out if I put in 1?

I'd appreciate it if you let me know Have a great day!

python

2022-09-20 10:29

1 Answers

>>> url = "http://sharebook.kr"

>>> list(url.split('o'))
['http://shareb', '', 'k.kr']

>>> spl = url.split('o')
>>> spl
['http://shareb', '', 'k.kr']

>>> spl[0]
'http://shareb'

>>> spl[1]
''

>>> spl[2]
'k.kr'

It's not that the value doesn't come out, but it's an empty string. Since oo followed by o, the first split is an empty (zero length) string.


2022-09-20 10:29

If you have any answers or tips


© 2022 pinfo. All rights reserved.