1. Introduction to string operations

In this series of intermediate Python, this is our second tutorial of String operations. So, if you have already gone through basic python tutorial then there we have discussed little about the string. If you want to check go to this link. Let me summarize what we have discussed previously.
  1. How to create strings?
  2. The string is immutable.
In this, will discuss different string methods.

2. Different methods of Strings

 

1. find(“string to be search”,beg,end)

It is the method of string which is used to search a particular string in our main string. It has 3 parameters -: string to be searched, from where should we start searching and till what index wants to search. Output -: 1. It returns the index where the first occurrence of string found and if the string doesn’t found it returns -1. Similarly, rfind() method works but the only difference is that it will return the last occurrence of that string.
stri = "Hello! Robofied will teach you ML, Robofied is an AI Comprehensive platform"
In [10]:
## Here we give only one parameter which is necessary i.e, the string to search and returning the index.
print("With one parameter")
print(stri.find("Robofied"))

## with beg parameters
print("With two parameter")
print(stri.find('Robofied',10))

## with beg and end parameter
print("With three parameter")
print(stri.find("Robofied",10,47))
Output
With one parameter
7
With two parameter
35
With three parameter
35

2. islower()

It is the method of string which checks if all the letters in the string are in lower case or not. Output -: It returns True or False Similarly, isupper() checks for upper case.
## Checking on previous strong
print(stri.islower())

##Defining another string
## Returns true if string is in lowercase.
str2 = "robofied"
print(str2.islower())
Output
False
True

3.startswith(“string”, beg, end)

It is the method of string which checks whether the main string starts with that particular string or not. It takes parameter string to be searched, a position from which we want to start to search and till what index we want to search. Output -:  It returns True if string found otherwise False. Similarly, endwith() will work.
str1 = "Hello! Robofied will teach you ML, Robofied is an AI Comprehensive platform"
In [6]:
## Checking if string starts with "He". 
print("With one parameters \n")
print(str1.startswith("He"))

## It is case sensitive
print(str1.startswith("he"))
Output
With one parameters 

True
False
In [13]:
## specifying string and beg parameter
print("With two parameter")
print(str1.startswith("Robofied",7))

## specifying string, beg and end parameter
print("\nWith three parameter")
print(str1.startswith("Robofied",7,42))
Output
With two parameter
True

With three parameter
True

4. len()

It will give the length of the string.
## printing the length of the string
print(len(str1))
Output
75

5. count(“string”,beg,end)

It is the method of string which counts the occurrence of the mentioned substring into the whole string. It takes 3 parameters string to search, beginning index, end index.
## Counting the occurence of string in the whole string
print("Without sspecifying begining and end")
print(stri.count("Robofied"))

## Counting the occurence of string in the whole string with specified begining.
print("With specifying begining")
print(stri.count("Robofied",5))

## Counting the occurence of string in the whole string with specified begining and end
print("With specifying begining and end")
print(stri.count("Robofied",5,26))
Output
Without sspecifying begining and end
2
With specifying begining
2
With specifying begining and end
1

6. isalpha()

It is the method of string which checks if all the letters in the string are alphabets. Output -: It returns true if all are alphabets otherwise false.
## Checking if whole string contains alphabets.
## Here we have special symbols that's why return False

print(stri.isalpha())
Output
False
In [24]:
## Creating new string with all alphabets

str2 = "heloo"
print(str2.isalpha())
Output
True

7. join()

It is the function of string which is used to join a sequence of strings mentioned in its arguments with the given string. It is the  – function which is used in many places. Output – It returns the whole concatenated string.
## Defining stringd
str3 = '_'
str4 = ['Hello','World','!']

## Joining the words in list with the specified string
str5 = str3.join(str4)
print(str5)        
Output
Hello_World_!

8. split(str)

It is the function of string which returns the returns a list of all words in a string using a str as a separator. It takes one argument which acts as a separator.
str5.split('_')
Output
['Hello', 'World', '!']

9. strip(str)

It is the method of string which removes all the trailing and leading mentioned string from the main string. It takes one argument which is the string which is to be stripped. Similarly, lstrip() and rstrip() works and they removed the string from either left or right respectively.
str6 = '___hello__world__'

## It removes the defined string from starting and ending but not in between!
str6.strip('_')
Output
'hello__world'

10. replace(“str to be replace”, “str replaced by”)

It is the method of string which is used to replace the string occurred in the whole string by new string. It takes 2 parameters string to be replaced and the string is replaced by. It has one more argument which is how many times the occurrence wants to replace.
## This function doesn't update the old string.
stri.replace("Robofied","Hello")
Output
'Hello! Hello will teach you ML, Hello is an AI Comprehensive platform'
In [36]:
stri
Output
'Hello! Robofied will teach you ML, Robofied is an AI Comprehensive platform'

11. format()

It is the function of string which is used for string formatting. String in format function contains {} curly braces which are replaced by the variable value passed in format function as parameters. It will be clear to you through example.
who = 'Ram'
what = 'apples'

## we are embedding our varible values in between
## Any number of {} can be used.
print('{} is purchasing these {}'.format(who, what))
Output
Ram is purchasing these apples