To compare two strings in a python if statement

Asked 2 years ago, Updated 2 years ago, 19 views

The title is a bit vague, but I'll explain it.

I'd like to give you a string as a condition in the Python if statement, but I'd like to give you two strings as a condition.

ex:

url = raw_input('>')
if 'http://' or 'https://' in url:
    print 'true'
else:
    print 'false'

For example, it's the same way as above. If I do it like that, it's always true.

>http://www.naver.com
true

>https://www.naver.com
true

>1234
true

It would be possible to write the if statement repeatedly, but I'm asking if there's a way to do it in one line.

python

2022-09-22 15:41

1 Answers

You seem to want to start with http:// or start with https://.

url = raw_input('>')
if url.startswith('http://') or url.startswith('https://'):
    print 'true'
else:
    print 'false'

Try it as above.


2022-09-22 15:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.