Python Index String Method

Опубликовано: 24 September 2015
на канале: Code master
16,865
97

Be sure to like, share and comment to show your support for our tutorials.

=======================================
Channel - https://goo.gl/pnKLqE
Playlist For This Tutorial - https://goo.gl/EyZFti
Latest Video - https://goo.gl/atWRkF
Facebook -   / mastercodeonline  
Twitter - https://twitter.com/mastercodeonlin?l...
Website - http://mastercode.online
======================================
Python Index String Method
In this Python tutorial we will take a look at the Python index string method. The index string method is very similar to the find string method. The index string method returns a value error where the find string method returns -1 if the argument is not found.

Python Index String Method Syntax
'String'.index('substring', Start Index, Stop Index)

Examples Of The Python Index String Method
Example 1:
a = 'This is a string'

Example 2:
a.index('s')
3

Example 3:
a.index('ing')
13

Example 4:
a.index('x')
Traceback (most recent call last):
File "stdin", line 1, in module
ValueError: substring not found

Example 5:
a.index('a', 7, 9)
8
Examples Explained

Example 1:

a = 'This is a string'- In this example we create a new string object and assign the object a variable.

Example 2:

a.index('s')- We call our string object and call the index string method on our string object. We provide an argument of 's' to our index string method.

3- We are returned 3 this where the first occurrence in the string object was found.
Example 3:

a.index('ing')- We call our string object and call the index string method on our string object. We provide an argument of 'ing' to our index string method.

8- We are returned 8 this is the first location that substring(our argument) was found.
Example 4:

a.index('x')- We call our string object and call the index string method on our string object. We provide an argument of 'x' to our string method.

Traceback (most recent call last):

File "stdin", line 1, in module

ValueError: substring not found - If the substring is not found then Python returns a ValueError.

Example 5:

a.index('a', 7, 9) - We call our string object via the variable and then we call our index string method with the argument 'a' and we also include to two optional arguments, 7 as the starting index and 9 as the ending index.

8- Returns 8 since is the location of the occurrence.
Conclusion

In this tutorial we have taken a look at the Python Index String Method. If you have any questions leave a comment below.


На этой странице сайта вы можете посмотреть видео онлайн Python Index String Method длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Code master 24 September 2015, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 16,865 раз и оно понравилось 97 зрителям. Приятного просмотра!