GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Books
  • Mini
  • Tips
  • Tags
  • About
  • learnbyexample

This view

  • Original page
  • New address

GeezerVison is showing learnbyexample.github.io — Python tip 1: tuple argument for startswith/endswith methods

You'd probably know about the startswith() and endswith() string methods.

>>> sentence = 'This is a sample string'

>>> sentence.startswith('This')
True
>>> sentence.startswith('is')
False

>>> sentence.endswith('ing')
True
>>> sentence.endswith('ly')
False

But did you know that you can also pass a tuple of strings?

>>> words = ['refuse', 'impossible', 'fire', 'present', 'read', 'shim']
>>> prefix = ('im', 're', 'use')

>>> [w for w in words if w.startswith(prefix)]
['refuse', 'impossible', 'read']

>>> [w for w in words if w.endswith(prefix)]
['refuse', 'fire', 'shim']

Video demo:


info See also my 100 Page Python Intro ebook.

#python#tip

← Python tip 2: membership operator


📰 Use this link for the Atom feed.
✅ Follow me on Twitter, GitHub and Youtube for interesting tech nuggets.


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.