String methods in python

sujitha rani j
4 min readJul 28, 2023

--

string methods are nothing but built-in python functions that allow us to work with strings. let us discuss some of the most commonly used string methods:

Syntax to apply string methods:

variable_name.method()

=> Here “ . ” will check whether variable is holding string or not.

=> Then it will apply the method on the string.

  1. upper() : This method is used to convert all the lowercase alphabets to uppercase alphabets.
var="ABcgdehG"
var.upper()

#output: 'ABCGDEHG'

2. lower() : This method is used to convert all the uppercase alphabets to lowercase alphabets.

var="ABcgdehG"
var.upper()

#output: 'abcgdehg'

3. capitalize() : This method is used to convert first alphabet of the entire string into uppercase and all other alphabets to lowercase.

var="data Analysis"
var.capitalize()

#output: 'Data analysis'

4. title() : This method is used to convert all the first alphabets of each word in the entire string to uppercase and all other to lowercase.

var="12python string methODS"
var.title()

#output: '12Python String Methods'

5. count() : This method is used to get the how many times a particular element or sequence of elements are repeated.

var="abcdabcdabcd"
var.count("a")
var.count("abcd")

#output: 3
#output: 3


#count of an element occurence in particular range
var1="abcdefabcef"
var1.count("a",0,3)

#output: 1

6. find() : This method is used to find the index value of an element on its first occurence.

var="python string methods"
var.find("t")

#output: 3

#it will return "-1" if the element is not in the string
var="python string methods"
var.find("1")

#output: -1

#to get the index of an element in particular range
var="python string methods"
var.find("t",6,11)

#output: 8

7. index() : This method is used to get the index value of an element at it’s first occurance. The main difference between find() and index() is index method will throw an error if we give an element which is not present in the string.

var="string methods"
var.index("t")

#output: 1

var="string methods"
var.index("t")

#output:
ValueError Traceback (most recent call last)
Cell In[68], line 2
1 #index( ) method
----> 2 x.index("12")

ValueError: substring not found

8. split() : This method is used to divide string into sub string using a separator. The default separator will be space “ ”.

#default separator
x1="python c++ java c"
x1.split()

#output: ['python', 'c++', 'java', 'c']

#using different separator
x1="python@c++ java c"
x1.split("@")

#output: ['python', 'c++ java c']

#using separator to get the sub strings in the given range
x1="python@c++@java@ c"
x1.split("@",2)

#output: ['python', 'c++', 'java@ c']

9. rsplit() : This method is used to divide string into sub strings from right to left.

x1="python@c++@java@ c"
x1.rsplit("@",1)

#output: ['python@c++@java', ' c']

10. swapcase() : This method is used to convert all the uppercase letters to lowercase and lowercase letters to uppercase.

s="STring"
s.swapcase()

#output: 'stRING'

11. strip() : This method is used to remove any particular element from starting and ending of a string.

var="@#!string methods#$@!"
var.strip("@!#")

#output: 'string methods#$'

12. replace() : this method is used to replace a single or sequence of characters with another single or sequence of characters.

var="sting methods"
var.replace("st","str")

#output: 'string methods'

13. maketrans() : This method is used to create a mapping table and we can use that table in translate() method.

14. translate() : This method is used to translate using the mapping table.

var="string methods in python"
x=var.maketrans("sio","@#$")
var.translate(x)

#output: '@tr#ng meth$d@ #n pyth$n'

15. startswith() : This method is used to check whether the string starts with given element or not.

var="string methods in python"
var.startswith("s")

#output: True

16. endswith() : This method is used to check whether the string ends with given element or not.

var="string methods in python"
var.startswith("on")

#output: True

17. isalpha() : This method is used to check whether the given string contains only alphabets or not.

var="stringmethodsinpython"
var.isalpha()

#output: True

var="string methods123 in python"
var.isalpha()

#output: False

18. isdigit() : This method is used to check whether the given string contains only digits or not.

var="s4528tring methods in python"
var.isdigit()

#output: False

var="123567899"
var.isdigit()

#output: True

19. islower() : This method is used to check whether the all the alphabets in the string are in lowercase or not.

var="s4528tring methods in python"
var.islower()

#output: True

var="12356789RHHSRYJ SSTRUI9"
var.islower()

#output: False

20. isupper() : This method is used to check whether the all the alphabets in the string are in uppercase or not.

var="PYTHON STRING METHODS"
var.isupper()

#output: True

var="PYthon STrings"
var.isupper()

#output: False

21. istitle() : This method is used to check the first alphabet of each word in the string is in uppercase and all other in lowercase or not.

var="Python String Methods"
var.istitle()

#output: True

var="Python StrinG MEthods"
var.istitle()

#output: False

22. isalnum() : This method will return true if the string contains only digits or only alphabets or combination of them and it will return false if it contains any special characters and others.

var="pythons12345"
var.istitle()

#output: True

var="python"
var.istitle()

#output: True

var="12357889"
var.istitle()

#output: True

var="Python 1234 "
var.istitle()

#output: False

--

--