Sitemap
5 min read4 days ago

--

STRING METHODS

What are String Methods in Python?

A string is a collection of characters enclosed within single quotes or double quotes or triple quotes.

Python String Methods:

  1. lower(): It is a string method, is used to convert all the upper case character to lower case and other characters are ignored. it returns always new string.
Press enter or click to view image in full size

2. Upper(): It is a string method, is used to convert all the lower case character to Upper case and other characters are ignored. it returns always new string.

3. Swap case(): It is a string method, is used to convert all the lower case to Upper case Upper case to lower case and other characters are ignored. it returns always new string.

Press enter or click to view image in full size

4. Capitalize() : It is a string method, is used to convert the first character(alphabet) or element ino upper case and rest of the character in lower case. It returns a new string.

Press enter or click to view image in full size

5. Title() : It is a string method, is used to convert the first character of a word inside a string into upper case and rest of the characters of the word into lower case and rest of characters will ignored.

Press enter or click to view image in full size

6. Count() : It is a string method, is used to count a specific character oe sequence of character occurred how many times in a string from left to right. It will give integer data type.

Press enter or click to view image in full size

7. Index() : It is a string method, is used to give you index value(integer) of a substring occurring at the first occurrence python moves left to right. Incase substring is not found will get error of value error.

i) rindex() : Is used to give you index value(integer) of a substring occurring at the last occurrence python moves right to left.

Press enter or click to view image in full size

8. Find() : It is a string method, is used to to give you the index value of a substring occurring at the first occurrence python moves left to right. Incase substring is not found will get ‘-1’.

i) rfind() : Is used to to give you the index value of a substring occurring at the last occurrence python moves right to left.

Press enter or click to view image in full size

NOTE: The Index () and find () methods are same methods. We can use starting index and ending index in index () and find () methods. Python will show only positive index.

9. Strip(): It is string method. Is used which we can remove the characters in the substring from the beginning and the end of the string. Note: Strip(substring) doesn’t follow a sequence, every characters are recognized separately, inside the strip(default value will be space). Strip() two types are follows as below:

i) rstrip() : Is used which we can remove the characters in the substring from the beginning.

Get Bhulakshmi Bcomp’s stories in your inbox

Join Medium for free to get updates from this writer.

ii) lstrip() : Is used which we can remove the characters in the substring from the ending.

Press enter or click to view image in full size

10. Split() : It is a string method. is using which we can divide or split an entire string into sub strings. Strip () has additionally two functions follow:

i) maxsplit () : It is additionally function to Split(). This can controls how many times your python engine has to work split(). Whenever we see the character moves left to right. If we can use maxsplit(-1) means it will split ’N’ number of times.

ii) rsplit() : It is additionally function to Split(). Whenever we see the character moves right to left.

Press enter or click to view image in full size

11. Replace() : It is a string method. We can replace any character or a sequence of character inside a string with other character.

Press enter or click to view image in full size

12. isupper() : It is a string method. Which is used to check if all the alphabets are in uppercase will get output True or False.

Press enter or click to view image in full size

13. islower() : It is a string method. Which is used to check if all the alphabets are in lowercase will get output True or False.

Press enter or click to view image in full size

14. istitle() : It is a string method. Which is used to check if first character of a word inside string should be uppercase, will get output True or False.

Press enter or click to view image in full size

15. isdigit() : It is a string method. Which is used to check characters of a inside string should be numbers not alphabets and special characters, will get output True or False.

Press enter or click to view image in full size

16. isalpha () : It is a string method. Which is used to check characters of a inside string should be alphabets and not numbers special characters, will get output True or False.

Press enter or click to view image in full size

17. isalnum () : It is a string method. Which is used to check characters of a inside string should be alphabets and numbers, not special characters, will get output True or False.

Press enter or click to view image in full size

18. startswith and endswith () : It is a string method. Which is used to check whether our string is starting and ending with substring or element. will get output True or False.

Press enter or click to view image in full size

19. partition () : It is a string method. this method splitting or dividing my string into exactly 3 parts. this separator also included. Note : (before value: separator value: after value).

i) rpartition () : It will split the string based on separator from right to left. But it collect values from left to right.

Press enter or click to view image in full size

These are Type conversions and string methods.

--

--