Hướng dẫn startswith in list python

I'm trying to avoid using so many if statements and comparisons and simply use a list, but not sure how to use it with str.startswith:

if link.lower().startswith("js/") or link.lower().startswith("catalog/") or link.lower().startswith("script/") or link.lower().startswith("scripts/") or link.lower().startswith("katalog/"):
    # then "do something"

What I would like it to be is:

if link.lower().startswith() in ["js","catalog","script","scripts","katalog"]:
    # then "do something"

Any help would be appreciated.

asked Dec 9, 2013 at 2:00

2

str.startswith allows you to supply a tuple of strings to test for:

if link.lower().startswith(("js", "catalog", "script", "katalog")):

From the docs:

str.startswith(prefix[, start[, end]])

Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for.

Below is a demonstration:

>>> "abcde".startswith(("xyz", "abc"))
True
>>> prefixes = ["xyz", "abc"]
>>> "abcde".startswith(tuple(prefixes)) # You must use a tuple though
True
>>>

answered Dec 9, 2013 at 2:01

6

You can also use any(), map() like so:

if any(map(l.startswith, x)):
    pass # Do something

Or alternatively, using a generator expression:

if any(l.startswith(s) for s in x)
    pass # Do something

Hướng dẫn startswith in list python

answered Dec 9, 2013 at 2:57

4



Hàm startswith() trong Python xác định xem chuỗi hoặc chuỗi con (nếu bạn cung cấp chỉ mục bắt đầu begin và chỉ mục kết thúc end) có bắt đầu với chuỗi con str không, nếu có trả về true, nếu không là false.

Nội dung chính

  • Syntax of String startswith()
  • startswith() Parameters
  • startswith() Return Value
  • Example 1: startswith() Without start and end Parameters
  • Example 2: startswith() With start and end Parameters
  • Passing Tuple to startswith()
  • Example 3: startswith() With Tuple Prefix
  • Description
  • Return Value


Cú pháp

Cú pháp của startswith() trong Python:

str.startswith(str, begin=0,end=len(string));

Các tham số:

  • str: Là chuỗi cần được kiểm tra.

  • begin: Tham số tùy ý này để thiết lập chỉ mục bắt đầu của boundary kết nối.

  • end: Tham số tùy ý này để thiết lập chỉ mục kết thúc của boundary kết nối.


Ví dụ sau minh họa cách sử dụng của startswith() trong Python.

str1 = "Vi du ham Python startswith"
print (str1.startswith('Vi'))
print (str1.startswith('du', 2, 4))
print (str1.startswith('Py', 2, 4))

Chạy chương trình Python trên sẽ cho kết quả:





Hàm startswith() trong Python xác định xem chuỗi hoặc chuỗi con (nếu bạn cung cấp chỉ mục bắt đầu begin và chỉ mục kết thúc end) có bắt đầu với chuỗi con str không, nếu có trả về true, nếu không là false.

Nội dung chính

  • Syntax of String startswith()
  • startswith() Parameters
  • startswith() Return Value
  • Example 1: startswith() Without start and end Parameters
  • Example 2: startswith() With start and end Parameters
  • Passing Tuple to startswith()
  • Example 3: startswith() With Tuple Prefix
  • Description
  • Return Value

Cú pháp

Cú pháp của startswith() trong Python:

str.startswith(str, begin=0,end=len(string));

Các tham số:

  • str: Là chuỗi cần được kiểm tra.

  • begin: Tham số tùy ý này để thiết lập chỉ mục bắt đầu của boundary kết nối.

  • end: Tham số tùy ý này để thiết lập chỉ mục kết thúc của boundary kết nối.


Ví dụ sau minh họa cách sử dụng của startswith() trong Python.

str1 = "Vi du ham Python startswith"
print (str1.startswith('Vi'))
print (str1.startswith('du', 2, 4))
print (str1.startswith('Py', 2, 4))

Chạy chương trình Python trên sẽ cho kết quả:



In this tutorial, we will learn about the Python String startswith() method with the help of examples.

The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False.

Example

message = 'Python is fun'

# check if the message starts with Python print(message.startswith('Python'))

# Output: True

Syntax of String startswith()

The syntax of startswith() is:

str.startswith(prefix[, start[, end]])

startswith() Parameters

startswith() method takes a maximum of three parameters:

  • prefix - String or tuple of strings to be checked
  • start (optional) - Beginning position where prefix is to be checked within the string.
  • end (optional) - Ending position where prefix is to be checked within the string.

startswith() Return Value

startswith() method returns a boolean.

  • It returns True if the string starts with the specified prefix.
  • It returns False if the string doesn't start with the specified prefix.

Example 1: startswith() Without start and end Parameters

text = "Python is easy to learn."

result = text.startswith('is easy')

# returns False print(result)

result = text.startswith('Python is ')

# returns True print(result)

result = text.startswith('Python is easy to learn.')

# returns True print(result)

Output

False
True
True

Example 2: startswith() With start and end Parameters

text = "Python programming is easy."

# start parameter: 7
# 'programming is easy.' string is searched

result = text.startswith('programming is', 7)

print(result) # start: 7, end: 18 # 'programming' string is searched

result = text.startswith('programming is', 7, 18)

print(result)

result = text.startswith('program', 7, 18)

print(result)

Output

True
False
True

Passing Tuple to startswith()

It's possible to pass a tuple of prefixes to the startswith() method in Python.

If the string starts with any item of the tuple, startswith() returns True. If not, it returns False


Example 3: startswith() With Tuple Prefix

text = "programming is easy"

result = text.startswith(('python', 'programming'))

# prints True print(result)

result = text.startswith(('is', 'easy', 'java'))

# prints False print(result) # With start and end parameter # 'is easy' string is checked

result = text.startswith(('programming', 'easy'), 12, 19)

# prints False print(result)

Output

True
False
False

If you need to check if a string ends with the specified suffix, you can use endswith() method in Python.



Description

Python string method startswith() checks whether string starts with str, optionally restricting the matching with the given indices start and end.

Syntax

Following is the syntax for startswith() method −

str.startswith(str, beg=0,end=len(string));

Parameters

  • str − This is the string to be checked.

  • beg − This is the optional parameter to set start index of the matching boundary.

  • end − This is the optional parameter to end start index of the matching boundary.

Return Value

This method returns true if found matching string otherwise false.

Example

The following example shows the usage of startswith() method.

#!/usr/bin/python

str = "this is string example....wow!!!";
print str.startswith( 'this' )
print str.startswith( 'is', 2, 4 )
print str.startswith( 'this', 2, 4 )

When we run above program, it produces following result −

True
True
False

python_strings.htm