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

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

Built-in Types
https://docs.python.org/3/library/stdtypes.html

内置类型
https://docs.python.org/zh-cn/3/library/stdtypes.html

1. 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. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.
如果字符串以指定的 prefix 开始则返回 True,否则返回 False。prefix 也可以为由多个供查找的前缀构成的元组。如果有可选项 start,将从所指定位置开始检查。如果有可选项 end,将在所指定位置停止比较。

prefix - 指定前缀,该参数可以是一个字符串或者是一个元素。
start - 可选参数,字符串中的开始位置索引,默认为 0。(可单独指定)
end - 可选参数,字符中结束位置索引。(不能单独指定)

2. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys
import numpy as np
import tensorflow as tf

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")

string_data = "cheng yong qiang!!!"
print(string_data.startswith('cheng'))
print(string_data.startswith('yong', 6))
print(string_data.startswith('qiang', 11, 16))
print(string_data.startswith('qiang', 11, 15))
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
True
True
True
False

Process finished with exit code 0

3. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys
import numpy as np
import tensorflow as tf

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))

print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")

string_data = "cheng yong qiang!!!"
print(string_data.startswith('c'))
print(string_data.startswith('ch'))
print(string_data.startswith('eng'))
print(string_data.startswith('y', 6))

print(string_data.startswith(('p', 'ch', 'k')))
print(string_data.startswith(('y', 'q', 'g')))
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
True
True
False
True
True
False

Process finished with exit code 0

你可能感兴趣的:(Python,3.x,-,Python,2.x)