String Operations in Python

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.

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.

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.

4. len()

It will give the length of the string.

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.

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.

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.

8. split(str)

It is the function of string which returns the returns a list of all words in a string using str as a separator. It takes one argument which acts as a separator.

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.

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.

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.

Close Menu