String methods in python
string is nothing but a sequences of characters present inside single double or tipple cotes. characters or nothing but numbers ,alphabets and special symbols .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:
properties of strings:
- Strings' are immutable.(once we create we can’t add replace and delete)
- Collection of characters which are present in sequence.
- Inside a string each characters have an index value
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.
y="surya"
y.upper()
#output: 'SURYA'2.lower() : This method is used to convert all the uppercase alphabets to lowercase alphabets.
y='PYTHON'
y.lower()
#output:"python"3. capitalize() : This method is used to convert first alphabet of the entire string into uppercase and all other alphabets to lowercase.
y="discord"
y.capitalize()
#output: 'Discord'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.
y="12 welcome to python"
y.title()
#output: '12 Welcme To Python '5. count() : This method is used to get the how many times a particular element or sequence of elements are repeated.
y="abcdabcdabcd"
y.count("a")
y.count("abcd")
#output: 3
#output: 3
#count of an element occurence in particular range
y1="abcdefabcef"
y1.count("a",0,3)
#output: 16. find() : This method is used to find the index value of an element on its first occurence.
y="python string methods"
y.find("t")
output: 3
#it will return "-1" if the element is not in the string
y="python string methods"
y.find("1")
#output: -1
#to get the index of an element in particular range
y="python string methods"
y.find("t",6,11)
#output: 87. 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.
z="string methods"
z.index("t")
#output: 1
z="string methods"
z.index("t")
#output:
ValueError Traceback (most recent call last)
Cell In[68], line 2
1 #index( ) method
----> 2 x.index("12")
ValueError: substring not found8. 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.
w1="python@c++@java@ c"
w1.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.
y="STring"
y.swapcase()
#output: 'stRING'11. strip() : This method is used to remove any particular element from starting and ending of a string.
y="@!#welcome to python programming #$@!"
y.strip("@!#")
#output: '!#welcome to python programming #$'12. replace() : this method is used to replace a single or sequence of characters with another single or sequence of characters.
l="sting methods"
l.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.
Get Psuryachinni’s stories in your inbox
Join Medium for free to get updates from this writer.
14. translate() : This method is used to translate using the mapping table.
m="string methods in python"
m=var.maketrans("sio","@#$")
var.translate(m)
#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.
d="string methods in python"
d.startswith("s")
#output: True16. endswith() : This method is used to check whether the string ends with given element or not.
p="string methods in python"
p.startswith("on")
#output: True17. isalpha() : This method is used to check whether the given string contains only alphabets or not.
var1="stringmethodsinpython"
var1.isalpha()
#output: True
var2="string methods123 in python"
var2.isalpha()
#output: False18. isdigit() : This method is used to check whether the given string contains only digits or not.
var1="s4528tring methods in python"
var1.isdigit()
#output: False
var1="123567899"
var1.isdigit()
#output: True19. islower() : This method is used to check whether the all the alphabets in the string are in lowercase or not.
z1="s4528tring methods in python"
z1.islower()
output: True
z2="12356789RHHSRYJ SSTRUI9"
z2.islower()
#output: False20. isupper() : This method is used to check whether the all the alphabets in the string are in uppercase or not.
x="PYTHON STRING METHODS"
x.isupper()
output: True
r="PYthon STrings"
r.isupper()
output: False21. 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: False22. 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







