Lesson 2 of 15

Strings

Strings

Python strings are immutable sequences of Unicode characters. They come with a rich set of built-in methods.

Common Methods

s = "  Hello, World!  "
s.upper()        # "  HELLO, WORLD!  "
s.lower()        # "  hello, world!  "
s.strip()        # "Hello, World!"
s.replace("World", "Python")  # "  Hello, Python!  "
s.split(", ")    # ["  Hello", "World!  "]
", ".join(["a", "b", "c"])    # "a, b, c"

Slicing

s = "Python"
s[0]     # "P"
s[-1]    # "n"
s[1:4]   # "yth"
s[::-1]  # "nohtyP"  (reversed)

Testing Content

"hello".startswith("he")  # True
"hello".endswith("lo")    # True
"ell" in "hello"          # True
"hello".isdigit()         # False
"123".isdigit()           # True

len() and count()

len("hello")          # 5
"hello".count("l")   # 2

Your Task

Implement is_palindrome(s) that:

  • Lowercases and strips s
  • Returns True if the string reads the same forwards and backwards, False otherwise
Pyodide loading...
Loading...
Click "Run" to execute your code.