Python - 056 : count(), find(), index() and rindex() functions in python

Опубликовано: 19 Июль 2023
на канале: The Digital Folks
273
2

Python String count()

The count() method returns the number of occurrences of a substring in the given string.

message = 'python is popular programming language'

number of occurrence of 'p'
print('Number of occurrence of p:', message.count('p'))

Output: Number of occurrence of p: 4

Python String find() method

Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1.



Syntax: str_obj.find(sub, start, end)

Parameters:

sub: Substring that needs to be searched in the given string.
start (optional): Starting position where the substring needs to be checked within the string.
end (optional): End position is the index of the last value for the specified range. It is excluded while checking.
Return: Returns the lowest index of the substring if it is found in a given string. If it’s not found then it returns -1.



5 Ways to Find the Index of a Substring in Python
str.find()
str.rfind()
str.index()
str.rindex()
re.search()


На этой странице сайта вы можете посмотреть видео онлайн Python - 056 : count(), find(), index() and rindex() functions in python длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь The Digital Folks 19 Июль 2023, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 273 раз и оно понравилось 2 зрителям. Приятного просмотра!