Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)
Concepts for this week:
In Python (and most programming in general), you start counting lists from the number 0. The first element in a list is “number 0”, the second is “number 1”, etc.
As a result, when you want to get single elements out of a list, you can ask a list for that number element:
There is also a convenient way to get sublists between two indices:
The first number is the “start index” and the last number is the “end index.”
You can also include a third number in the indexing, to count how often you should read from the list:
To read the whole list, just use the variable name (in the above examples, a
), or you can also use [:]
at the end of the variable name (in the above examples, a[:]
).
Because strings are lists, you can do to strings everything that you do to lists. You can iterate through them:
Will give the result:
You can take sublists:
Now s
has the string “examp” in it.
Moral of the story: a string is a list.