Searching of String in Python.|methods|sub-strings.

Searching of String.

This is the process of checking occurance of substring in whole string or in a substring finding the characyers if starting index BEG and End ending index are given.

Syntax: str_var.find("string",beg=0,end=len(string)) where string specify,string to be search beg specify starting index by default it is 0. end specify is ending index by default it is length of string.

Here, return value of this function will be an index if found or -1 otherwise.

Example:

str="python" print(str) print(str.find("y"))

output:

python 1

Various methods of string class is used to search the substring in a given string.

bool endswith(str1,str2)

str="python basic" print(str) print(str.endswith("basic"))

output:

python basic True

bool startswith(str1,str2)

str="python basic" print(str) print(str.startswith("python"))

output:

python basic True

int find(str1,str2)

str="python basic" print(str) print(str.find("python")) print(str.find("language"))

output:

python basic 0 -1

int rfind(str1,str2)

str="python basic" print(str) print(str.rfind("a"))

output:

python basic 8

int count(str1 str)

str="python basic language" print(str) print(str.count("a"))

output:

python basic language 3

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.