STRING METHODS
What is string methos?
- These are the methods which we can apply only on string data.
- In python all the methods are called as attributes. If there is any error in any method, it shows Attribute error.
Syntax: variable name. method name ()
- Upper():
- It is used to convert all the lower-case characters into upper-case characters and ignore remaining characters.
- It will not replace an existing string; it creates a new string.
Syntax: variable name. upper ()
Ex:
2. Lower():
- It is used to convert all the upper-case characters into lower-case characters and ignore remaining characters.
- It will not replace an existing string; it creates a new string.
Syntax: variable name. lower ()
Ex:
3. Swapcase():
- It converts lower case characters to upper case or upper-case characters to lower case and remaining characters will be ignored.
- It will not replace an existing string; it returns a new string.
Syntax: variable name. swapcase ()
Ex:
4. Capitalize():
- It converts the first element of a string into upper case character if it is a lower-case character, it ignores remaining characters.
- If the first character is an upper-case character, it returns same string.
Syntax: variable name. capitalize ()
Ex:
5. Title():
- It converts first letter of every word into a capital letter and remaining will be smaller letters.
- It returns new string.
Syntax: variable name. title ()
Ex:
6. Count():
- It is used to count how many times a character or a sequence of charcaters is occuring inside a string.
- It returns number not string.
Syntax: variable name. count (“substring”)
Syntax of count () with range:
→ variable name. count (“substring” , start index , end index )
Ex:
7.Index():
- It is used to find the first occurance of a substring not only for a single character (“a”), also for a sequence of characters (“abc”).
- If element or substring not present, index() returns value error.
Syntax: variable name. index (“substring”)
Syntax of index () with range:
→ variable name. index(“substring” , start index , end index )
Ex:
8. Find():
- It is used to find the first occurance of a substring not only for a single character (“a”), also for a sequence of characters (“abc”).
- Both index() and find() are same but only one difference, If element or substring not present, index() returns value error but find() returns -1.
Syntax: variable name. find(“substring”)
Syntax of find() with range:
→ variable name. find(“substring” , start index , end index )
Ex:
9.rindex():
- Same as index(), but i checks from right to left.
- In this start_index and end_index must be from left to right but output counts from right to left.
Syntax: variable name. rindex (“substring”)
Syntax of rindex () with range:
→ variable name. rindex(“substring” , start index , end index )
Ex:
10. rfind():
- Same as find(), but i checks from right to left.
- In this start_index and end_index must be from left to right but output counts from right to left.
- If start_index and end_index not from left to right it returns -1.
Syntax: variable name. rfind(“substring”)
Syntax of rfind() with range:
→ variable name. rfind(“substring” , start index , end index )
Ex:
11. Replace():
- It is used to replace a substring with an another string inside a string.
Syntax: variable name. replace(“substring” , “string”)
Syntax of replace() with maxium_limit:
→ variable name. replace(“substring” , “string” , max_limit)
Ex:
12. Strip():
- It is used to remove any substring from the starting and ending position.
Syntax: variable name. strip(“substring”)
Ex:
13. lstrip():
- It removes character from starting position.
Syntax: variable name. lstrip(“substring”)
Ex:
14. rstrip():
- It removes character from ending position.
Syntax: variable name. rstrip(“substring”)
Ex:
15. Split():
- It is used to divide or split the entire string into substring based on substring.
- Default substring is space( ).
- It will perform split from left to right and creates the group from left to right.
- If there is nothing left to split it returns empty string.
- Inside split not use empty seperator, Ex: var.split().
Syntax: variable name. split(“substring”)
Get Ajay Danthuluri’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax of split() with maximum number of splits:
→ variable name. split(“substring” , number(used to control number of splits))
Ex:
16. rsplit():
- It will perform split from right to left and creates the group from left to right.
Syntax: variable name. rsplit(“substring”)
Syntax of rsplit() with maximum number of splits:
→ variable name. rsplit(“substring” , number(used to control number of splits)))
Ex:
17.Partition():
- It is used to split or divide the entire string into three parts.
- If there is an empty string it returns three empty strings.
Syntax: variable name. partiton(“before value of split” , “actual value of split” , “after value of split”)
Ex:
18. Center():
- It is used to make the string appear approximately at the center of new string.
Syntax: variable name. center(length of new string)
Syntax: variable name. center(length of new string , “character which we want oresent at starting and ending of string”)
Ex:
19. Zfill():
- It fills starting element as zero (‘0’).
- It used in NLP.
Syntax: variable name. zfill(number (length of new string))
Ex:
20. Maketrans():
- It creates a mapping table.
- input — — — maps — — — output.
- Both input and output string length should be same.
Syntax: str.maketrans( string(input) , string(output))
Ex:
21. Translate():
- It shows or returns mapping table.
Syntax: var_name. translate(mapping table name)
Ex:
22. isupper():
- It checks whether all the alphabets inside the string is upper case or not.
- If all are upper case it returns True, otherwise False.
Syntax: var_name. isupper()
Ex:
23. islower():
- It checks whether all the alphabets inside the string is lower case or not.
- If all are lower case it returns True, otherwise False.
Syntax: var_name. islower()
Ex:
24. istitle():
- It checks whether every words first alphabet is upper case or not and remainig are lower case or not.
- If correct returns True, otherwise False.
Syntax: var_name. istitle()
Ex:
25. isdigit():
- It checks all the elements in a string are numbers or not.
- If correct returns True, otherwise False.
Syntax: var_name. isdigit()
Ex:
26. isalpha():
- It checks all the elements in a string are alphabets or not.
- If correct returns True, otherwise False.
Syntax: var_name. isalpha()
Ex:
27. isalnum():
- It checks all the elements in a string either alphabets or numbers or not.
- If correct returns True, otherwise False.
Syntax: var_name. isalnum()
Ex:
28. isspace():
- It checks all the elements in a string are spaces or not.
- If correct returns True, otherwise False.
Syntax: var_name. isspace()
Ex:
29. isidentifier():
- It checks whether the string is valid identifier or not by using naming convention means string starts with (a-z) or (0–9) or (_).
- If correct returns True, otherwise False.
Syntax: var_name. isidentifier()
Ex:
30. Startswith():
- It checks whether the string starts with the given substring or not.
- If correct returns True, otherwise False.
Syntax: var_name. startswith(“substring”)
Ex:
31. Endswith():
- It checks whether the string ends with the given substring or not.
- If correct returns True, otherwise False.
Syntax: var_name. endswith(“substring”)
Ex:
32. Format():
- It is used to combine a string data with other data type which gives a new string data.
Syntax: var_name. format(value1,value2,…,value n)
Ex:









