我们定义一段字符串进行测试
Test = '''Life is short. Learn Python early! Life is short. Learn Python early'''
方法和使用示例 | 说明 | 结果 |
print(len(Test)) | 字符串长度 | 68 |
print(Test.startswith('Life')) | 以指定字符串开头 | True |
print((Test.endswith('early'))) | 以指定字符串结尾 | True |
print(Test.find('short')) | 第一次出现字符串的下标 | 8 |
print(Test.rfind('short')) | 最后一次出现字符串的下标 | 43 |
print(Test.count('is')) | 字符串出现几次 | 2 |
print(Test.isalnum()) | 所有字符全是字母或数字 | False |
print('Python' in Test) | 指定字符串在Test中 | True |
print('Python' not in Test) | 指定字符串不在Test中 | False |