String object methods in python

Previous
Next

max(): It returns the max alphabetical character from the string str.

s1="tutipy"
print(max(s1))
s2="Tutipy tutorials"
print(max(s2))

replace(): It returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.

str = "Tutipy site"
print(str.replace("site", "Tutorials"))

title(): Returns a copy of the string in which first characters of all the words are capitalized.

s= "python tutorials website";
print(s.title())

swapcase(): It converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.

s = "TUTIPY TUTORIALS."
print(s.swapcase())
s="tutipy website."
print(s.swapcase())
s= "Tutipy WebSite."
print(s.swapcase())

find(): Returns index of first occurrence of the given substring. If it is not available then we will get -1

s="Learning Python is Simpler"
print(s.find("Python")) 
print(s.find("Tutipy")) 

index(): It returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception.

s='Python programming is fun'
print(s.index('is fun'))
print(s.index('ing', 10))
print(s.index('g is', 10, -4))

count(): It returns the number of occurrences of a substring in the given string.

s = "Tutipy tutorials site"
print(s.count('t'))
print(s.count('u',1))
print(s.count('t',10,20))

split(): It splits a string into a list.

s="This is python tutorials website"
print(s.split(" "))
Previous
Next

Add Comment

Courses Enquiry Form