字符串startswith_Python字符串startswith()

字符串startswith

Python string startswith() function returns True if the string starts with the given prefix, otherwise it returns False.

如果字符串以给定的前缀开头,则Python字符串startswith()函数将返回True ,否则返回False

Python字符串startswith() (Python String startswith())

This function syntax is:

该函数语法为:

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

The prefix can be a string or a tuple of string prefixes to look for in the string.

前缀可以是字符串,也可以是要在字符串中查找的字符串前缀的元组

The start is an optional argument to specify the index from where test starts.

start是一个可选参数,用于指定从何处开始测试的索引。

The end is an optional argument to specify the index where the test has to stop.

end是一个可选参数,用于指定测试必须停止的索引。

Python字符串以示例开头 (Python string starts with example)

Let’s look at a simple example of python string startswith() function.

让我们看一个简单的python字符串startswith()函数示例。

s = 'Python is Awesome'

# without start and end
print(s.startswith('Python'))

Let’s look at some examples with the start argument.

让我们来看一些带有start参数的示例。

s = 'Python is Awesome'

print(s.startswith('Python', 3))
print(s.startswith('hon', 3))

Output:

输出:

False
True

Since the start index is 3, the test will use the sub-string “hon is Awesome”. That’s why the first output is False and the second one is True.

由于起始索引为3,因此测试将使用子字符串“ hon is Awesome”。 这就是为什么第一个输出为False而第二个输出为True的原因。

Let’s look at some examples with the start and end arguments.

让我们来看一些带有start和end参数的示例。

s = 'Python is Awesome'

print(s.startswith('is', 7, 10))
print(s.startswith('Python is', 0, 10))
print(s.startswith('Python is', 0, 6))

Output:

输出:

True
True
False

For the first print statement, the substring is “is the ” i.e. starting with “is” and hence the output is True.

对于第一个print语句,子字符串为“ is”,即以“ is”开头,因此输出为True。

In the second print statement, the substring is “Python is A” and hence the output is True.

在第二个打印语句中,子字符串为“ Python is A”,因此输出为True。

For the third print statement, the substring is “Python” that doesn’t start with “Python is”, hence the output is False.

对于第三个print语句,子字符串为“ Python”,而不以“ Python is”开头,因此输出为False。

Python字符串startswith()示例与元组 (Python string startswith() example with Tuple)

Let’s look at some examples with Tuple of strings as the prefix.

让我们看一些以字符串元组为前缀的示例。

s = 'Python is Awesome'

print(s.startswith(('is', 'Python')))
print(s.startswith(('is', 'hon'), 7))

Output:

输出:

True
True

For the first print statement, string starts with “Python” and hence the output is True.

对于第一个打印语句,字符串以“ Python”开头,因此输出为True。

For the second print statement, string test begins from index position 7. So the substring is “is Awesome” which starts with “is” and hence the output as True.

对于第二个打印语句,字符串测试从索引位置7开始。因此子字符串是“ is Awesome”,以“ is”开头,因此输出为True。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23821/python-string-startswith

字符串startswith

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值