Python3教學
Python3引入什麼新的東西?
Python3環境安裝設置
Python3基礎語法
Python3變量類型
Python算術運算符實例
Python3比較運算符實例
Python3賦值運算符實例
Python3位運算符實例
Python3邏輯運算符實例
Python3成員運算符實例
Python3標識運算符實例
Python3運算符優先級示例
Python3基本運算符
Python3 if語句
Python3 if...elseif...else語句
Python3嵌套if語句
Python3決策
Python3 while循環語句
Python3 for循環語句
Python3嵌套循環
Python3 break語句
Python3 continue語句
Python3 pass語句
Python3循環
Python3 abs()函數
Python3 ceil()函數
Python3 exp()函數
Python3 fabs()函數
Python3 floor()函數
Python3 log()函數
Python3 log10()函數
Python3 max()函數
Python3 min()函數
Python3 modf()函數
Python3 pow()函數
Python3 round()函數
Python3 sqrt()函數
Python3 choice()函數
Python3 randrange()函數
Python3 random()函數
Python3 seed()函數
Python3 shuffle()函數
Python3 uniform()函數
Python3 acos()函數
Python3 asin()函數
Python3 atan()函數
Python3 atan2()函數
Python3 cos()函數
Python3 hypot()函數
Python3 sin()函數
Python3 tan()函數
Python3 degrees()函數
Python3 radians()函數
Python3數字
Python3 string.capitalize()方法
Python3 string.center()方法
Python3 string.count()方法
Python3 string.decode()方法
Python3 string.endswith()方法
Python3 string.expandtabs()方法
Python3 string.encode()方法
Python3 string.find()方法
Python3 string.index()方法
Python3 string.isalnum()方法
Python3 string.isalpha()方法
Python3 string.isdigit()方法
Python3 string.tolower()方法
Python3 string.isnumeric()方法
Python3 string.isspace()方法
Python3 string.istitle()方法
Python3 string.isupper()方法
Python3 string.join()方法
Python3 string.len()方法
Python3 string.ljust()方法
Python3 string.lower()方法
Python3 string.lstrip()方法
Python3 string.maketrans()方法
Python3 string.max()方法
Python3 string.min()方法
Python3 string.replace()方法
Python3 string.rfind()方法
Python3 string.rindex()方法
Python3 string.rjust()方法
Python3 string.rstrip()方法
Python3 string.split()方法
Python3 string.splitlines()方法
Python3 string.startswith()方法
Python3 string.strip()方法
Python3 string.swapcase()方法
Python3 string.title()方法
Python3 string.translate()方法
Python3 string.upper()方法
Python3 string.zfill()方法
Python3 string.isdecimal()方法
Python3字符串
Python3 list.cmp()方法
Python3 list.len()方法
Python3 list.max()方法
Python3 list.min()方法
Python3 list.list()方法
Python3 list.append()方法
Python3 list.count()方法
Python3 list.extend()方法
Python3 list.index()方法
Python3 list.insert()方法
Python3 list.pop()方法
Python3 list.remove()方法
Python3 list.reverse()方法
Python3 list.sort()方法
Python3列表
Python3 tuple.cmp()方法
Python3 tuple.len()方法
Python3 tuple.max()方法
Python3 tuple.min()方法
Python3 tuple.tuple()方法
Python3元組
Python3 dictionary.cmp()方法
Python3 dictionary.len()方法
Python3 dictionary.str()方法
Python3 dictionary.type()方法
Python3 dictionary.clear()方法
Python3 dictionary.copy()方法
Python3 dictionary.fromkeys()方法
Python3 dictionary.get()方法
Python3 dictionary.has_key()方法
Python3 dictionary.items()方法
Python3 dictionary.keys()方法
Python3 dictionary.setdefault()方法
Python3 dictionary.update()方法
Python3 dictionary.values()方法
Python3字典
Python3 time.altzone()方法
Python3 time.asctime()方法
Python3 time.clock()方法
Python3 time.ctime()方法
Python3 time.gmtime()方法
Python3 time.localtime()方法
Python3 time.mktime()方法
Python3 time.sleep()方法
Python3 time.strftime()方法
Python3 time.time()方法
Python3 time.tzset()方法
Python3日期和時間
Python3函數
Python3模塊
Python3 file.close()方法
Python3 file.tell()方法
Python3 file.truncate()方法
Python3 file.write()方法
Python3 file.writelines()方法
Python3 file.flush()方法
Python3 file.fileno()方法
Python3 file.isatty()方法
Python3 file.next()方法
Python3 file.read()方法
Python3 file.readline()方法
Python3 file.readlines()方法
Python3 file.seek()方法
Python3文件方法
Python3 os.access()方法
Python3 os.chdir()方法
Python3 os.chflags()方法
Python3 os.chmod()方法
Python3 os.chown()方法
Python3 os.chroot()方法
Python3 os.close()方法
Python3 os.closerange()方法
Python3 os.dup()方法
Python3 os.dup2()方法
Python3 os.fchdir()方法
Python3 os.fchmod()方法
Python3 os.fchown()方法
Python3 os.fdatasync()方法
Python3 os.fopen()方法
Python3 os.fpathconf()方法
Python3 os.fstat()方法
Python3 os.fstatvfs()方法
Python3 os.fsync()方法
Python3 os.ftruncate()方法
Python3 os.getcwd()方法
Python3 os.getcwdu()方法
Python3 os.isatty()方法
Python3 os.lchflags()方法
Python3 os.lchmod()方法
Python3 os.lchown()方法
Python3 os.link()方法
Python3 os.listdir()方法
Python3 os.lseek()方法
Python3 os.lstat()方法
Python3 os.major()方法
Python3 os.makedev()方法
Python3 os.makedirs()方法
Python3 os.minor()方法
Python3 os.mkdir()方法
Python3 os.mkfifo()方法
Python3 os.mknod()方法
Python3 os.open()方法
Python3 os.openpty()方法
Python3 os.pathconf()方法
Python3 os.pipe()方法
Python3 os.popen()方法
Python3 os.read()方法
Python3 os.readlink()方法
Python3 os.remove()方法
Python3 os.removedirs()方法
Python3 os.rename()方法
Python3 os.renames()方法
Python3 os.rmdir()方法
Python3 os.stat()方法
Python3 os.stat_float_times()方法
Python3 os.statvfs()方法
Python3 os.symlink()方法
Python3 os.tcgetpgrp()方法
Python3 os.tcsetpgrp()方法
Python3 os.tempnam()方法
Python3 os.tmpfile()方法
Python3 os.tmpnam()方法
Python3 os.ttyname()方法
Python3 os.unlink()方法
Python3 os.utime()方法
Python3 os.walk()方法
Python3 os.write()方法
Python3 os文件目錄的方法
Python3文件操作
Python3標準異常
Python3斷言
Python3異常處理

Python3 string.index()方法

它確定字符串str是否由給出的開始索引beg和結束索引end,出現在字符串或字符串的子串中。這個方法與 find()方法相似,但如果沒有找到子字符串則會引發異常。

語法

str.index(str, beg=0 end=len(string))

參數

  • str -- 此選項指定要搜索的字符串

  • beg -- 這是起始索引,默認情況下是0

  • end -- 這是結束的索引,默認情況下其等於該字符串的長度。

返回值

如果 str 找到則返回索引,如果找不到將引發異常。

示例

#!/usr/bin/python3

str1 = "this is string example....wow!!!"
str2 = "exam";

print (str1.index(str2))
print (str1.index(str2, 10))
print (str1.index(str2, 40))

結果

15
15
Traceback (most recent call last):
File "test.py", line 7, in
print (str1.index(str2, 40))
ValueError: substring not found

shell returned 1