{"id":"elif-comparing-strings","slug":"comparing-strings","title":"Comparing strings","text":"You have compared strings with `==` and `!=` already. Two strings are equal only when they have exactly the same characters, capital letters and spaces included:\n\n```pycon\n>>> \"Python\" == \"python\"\nFalse\n>>> \"yes\" == \"yes \"\nFalse\n>>> \"cat\" != \"dog\"\nTrue\n```\n\n### Alphabetical order\n\n`<` and `>` work on strings too. A string is \"less than\" another when it comes first in alphabetical order, as in a dictionary:\n\n```pycon\n>>> \"apple\" < \"banana\"\nTrue\n>>> \"cat\" > \"car\"\nTrue\n>>> \"car\" < \"cart\"\nTrue\n```\n\nPython compares two strings character by character from the start, and the first character that differs decides. `\"cat\"` and `\"car\"` differ at the third letter, and `t` comes after `r`. When one string runs out first, as `\"car\"` does next to `\"cart\"`, the shorter one comes first.\n\nTogether with `elif`, this puts two words in order:\n\n```python\nfirst = input(\"First word: \")\nsecond = input(\"Second word: \")\nif first < second:\n    print(f\"{first} comes before {second}\")\nelif first > second:\n    print(f\"{second} comes before {first}\")\nelse:\n    print(\"The words are the same\")\n```\n\n```input\npear\napple\n```\n\n```output\nFirst word: pear\nSecond word: apple\napple comes before pear\n```\n\n### Capitals and digits\n\nPython's order isn't quite a dictionary's. It compares the codes of the characters, and in them every capital letter comes before every small letter. Letters with marks, such as `é`, come after all of these. Digits in a string are just characters as well, compared one at a time:\n\n```pycon\n>>> \"Zebra\" < \"apple\"\nTrue\n>>> \"10\" < \"9\"\nTrue\n>>> 10 < 9\nFalse\n```\n\n`\"10\"` comes before `\"9\"` because its first character, `1`, comes before `9`. So compare words written the same way (all in small letters, for example), and convert numbers that the user typed with `int` before you compare them.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"print(\"dog\" < \"cat\")\nprint(\"Dog\" < \"cat\")\nprint(\"pen\" < \"pencil\")\nprint(\"7\" > \"10\")\n","solution":"print(\"dog\" < \"cat\")\nprint(\"Dog\" < \"cat\")\nprint(\"pen\" < \"pencil\")\nprint(\"7\" > \"10\")\n","hints":["`d` comes after `c` in the alphabet, so `\"dog\"` is not less than `\"cat\"`.","Every capital letter comes before every small letter.","`\"pen\"` runs out first, so it comes first. And `\"7\"` and `\"10\"` are strings: their first characters `7` and `1` decide."],"tests":[{"name":"Output","stdin":"","expect":"False\nTrue\nTrue\nTrue","hidden":false}],"require":[],"forbid":[],"lines":["False","True","True","True"],"more":[{"task":"In a word-guessing game, the secret word is `maple`, and the game gives hints by alphabetical order. Write a program that asks for a guess (in small letters). If the guess comes before the secret word, it prints `My word comes later.`, if it comes after it, `My word comes earlier.`, and if it's the secret word, `You found it!`\n\n```\nYour guess: tiger\nMy word comes earlier.\n```\n","kind":"code","seed":"secret = \"maple\"\nguess = input(\"Your guess: \")\n","solution":"secret = \"maple\"\nguess = input(\"Your guess: \")\nif guess < secret:\n    print(\"My word comes later.\")\nelif guess > secret:\n    print(\"My word comes earlier.\")\nelse:\n    print(\"You found it!\")\n","hints":["`guess < secret` is true when the guess comes earlier in alphabetical order than the secret word.","If the guess comes first, the secret word comes later. Check the other direction with `elif guess > secret:`.","The only case left over is the secret word itself, so it goes in the `else`."],"tests":[{"name":"Test 1","stdin":"tiger\n","expect":"Your guess: tiger\nMy word comes earlier.","hidden":false},{"name":"Test 2","stdin":"apple\n","expect":"Your guess: apple\nMy word comes later.","hidden":true},{"name":"Test 3","stdin":"maple\n","expect":"Your guess: maple\nYou found it!","hidden":true},{"name":"Test 4","stdin":"map\n","expect":"Your guess: map\nMy word comes later.","hidden":true},{"name":"Test 5","stdin":"maples\n","expect":"Your guess: maples\nMy word comes earlier.","hidden":true},{"name":"Test 6","stdin":"melon\n","expect":"Your guess: melon\nMy word comes earlier.","hidden":true}],"require":[],"forbid":[]},{"task":"A library shelves its books by the author's last name, in small letters. Names that come before `h` in alphabetical order go on shelf A, names from `h` up to (but not including) `p` on shelf B, and the rest on shelf C. Write a program that asks for the name and prints the shelf.\n\n```\nAuthor's last name: orwell\nShelf B\n```\n","kind":"code","seed":"name = input(\"Author's last name: \")\n","solution":"name = input(\"Author's last name: \")\nif name < \"h\":\n    print(\"Shelf A\")\nelif name < \"p\":\n    print(\"Shelf B\")\nelse:\n    print(\"Shelf C\")\n","hints":["Compare the whole name with a one-letter string: every name that starts with a to g is less than `\"h\"`.","Check the shelves in order: `if name < \"h\":`, then `elif name < \"p\":`, then `else`.","A name such as `hugo` is not less than `\"h\"` (the shorter string comes first), so it goes to shelf B, as it should."],"tests":[{"name":"Test 1","stdin":"orwell\n","expect":"Author's last name: orwell\nShelf B","hidden":false},{"name":"Test 2","stdin":"austen\n","expect":"Author's last name: austen\nShelf A","hidden":true},{"name":"Test 3","stdin":"hugo\n","expect":"Author's last name: hugo\nShelf B","hidden":true},{"name":"Test 4","stdin":"pratchett\n","expect":"Author's last name: pratchett\nShelf C","hidden":true},{"name":"Test 5","stdin":"gaiman\n","expect":"Author's last name: gaiman\nShelf A","hidden":true},{"name":"Test 6","stdin":"tolkien\n","expect":"Author's last name: tolkien\nShelf C","hidden":true}],"require":[],"forbid":[]}]}